热线电话:13121318867

登录
2019-03-13 阅读量: 557
python如何随机选取类?

每个项目都是一行,在“类”下,我们可以看到项目的分类位置。要素名称下的值(“高度”等)是项目对该要素的值。所有值和功能都以逗号分隔。

我们将项目保存到名为items的列表中,其元素是字典(每个项目一个)。这些项目词典的关键是功能名称,以及用于保存项目类的“类”。最后,我们想要对列表中的项目进行随机播放(这是一项安全措施,以防项目处于奇怪的顺序)。

我们将从文件中读取(名为“data.txt”),我们将按行分割输入:

f = open('data.txt','r');
lines = f.read()。splitlines();
f.close();

该文件的第一行包含功能名称,末尾带有关键字“Class”。我们希望将功能名称存储到列表中:

items = [];

for i in range(1, len(lines)):

line = lines[i].split(', ');

itemFeatures = {"Class" : line[-1]};

# Iterate through the features

for j in range(len(features)):

# Get the feature at index j

f = features[j];

# The first item in the line

# is the class, skip it

v = float(line[j]);

# Add feature to dict

itemFeatures[f] = v;

# Append temp dict to items

items.append(itemFeatures);

shuffle(items);

11.8611
6
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子