在我的代码中,用户输入一个文本文件,该文件保存为变量“emplaced\u animals\u data”。该变量有四列(Animal ID、X location、Y location和Z location),行数根据上载的文本文件而异。然后我有另一个列表(列出的动物),其中包含我们要从安置的动物数据中收集位置数据的动物。到目前为止,我已经为列表中的每个项目创建了一个新变量。我希望能够将这些新变量中的每一个与我安置的\u items \u data Animal ID列进行比较,并存储它们的适当位置,而不必显式调用“Animal1,Animal2,etc.”下面是我当前拥有的代码和输出的内容:
listed_animals = ['cat', 'dog', 'bear', 'camel', 'elephant']
Animal1_Xloc = []
Animal1_Yloc = []
Animal1_Zloc = []
for i, value in enumerate(listed_animals):
for j in range(0, len(emplaced_animals_data)):
exec ("Animal%s=value" % (i))
if Animal1 == emplaced_animals_data[j,0]: #don't want to explicitly have to call
Animal1_Xloc = np.append(Animal1_Xloc, emplaced_animals_data[j,1])
Animal1_Yloc = np.append(Animal1_Yloc, emplaced_animals_data[j,2])
Animal1_Zloc = np.append(Animal1_Zloc, emplaced_animals_data[j,3])
print(Animal1)
print('X locations:', Animal1_Xloc)
print('Y locations:', Animal1_Yloc)
print('Z locations:', Animal1_Zloc)
dog
X locations: ['1' '2' '3' '4' '1' '2' '3' '4' '1' '2' '3' '4' '1' '2' '3' '4' '1' '2'
'3' '4']
Y locations: ['3' '12' '10' '8' '3' '12' '10' '8' '3' '12' '10' '8' '3' '12' '10' '8'
'3' '12' '10' '8']
Z locations: ['9' '8' '1' '1' '9' '8' '1' '1' '9' '8' '1' '1' '9' '8' '1' '1' '9' '8'
'1' '1']
安置动物数据列表中使用的数据可在以下位置找到: emplaced_animals_data visual
我的目标是用不同的符号绘制每种动物的位置,但由于列出的动物列表中可能并不总是有相同的动物或相同数量的动物,所以我不能明确地称每种动物为。有什么想法可以让这个迭代?
请看下面的代码,我用随机数生成了自己的数据来模拟您的数据。这只是对你的另一个问题的numpy列表的一个小小的修改:
相关问题 更多 >
编程相关推荐