从列表比较和绘图中获取位置数据

在我的代码中,用户输入一个文本文件,该文件另存为变量“ emplaced_animals_data”。此变量有四列(动物ID,X位置,Y位置和Z位置),并且行数取决于上传的文本文件。然后,我还有另一个列表(listed_animals),其中包含我们要从emplaced_animals_data收集位置数据的动物。到目前为止,我已经为listed_animals列表中的每个项目创建了一个新变量。我希望能够将这些新变量中的每一个与我的emplaced_items_data动物ID列进行比较,并存储它们的适当位置,而不必显式调用“ Animal1,Animal2等”。这是我当前拥有的代码以及正在输出的代码:

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,2])
            Animal1_Zloc = np.append(Animal1_Zloc,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列表中使用的数据可以在这里找到: emplaced_animals_data visual

我的目标是用不同的符号绘制每只动物的位置,但是由于list_animals列表中的动物可能并不总是相同或数量相同,因此我无法明确地称呼每个动物。那么关于如何进行迭代的任何想法?

whxdhrwhxdhr 回答:从列表比较和绘图中获取位置数据

请参见下面的代码,我使用随机数生成了自己的数据以模仿您的数据。这只是对您使用其他问题中的numpy列表所做的微小修改:

import numpy as np

# note that my delimiter is a tab,which might be different from yours
emplaced_animals = np.genfromtxt('animals.txt',skip_header=1,dtype=str,delimiter='   ')
listed_animals = ['cat','dog','bear','camel','elephant']

def get_specific_animals_from(list_of_all_animals,specific_animals):
    """get a list only containing rows of a specific animal"""
    list_of_specific_animals = np.array([])
    for specific_animal in specific_animals:
        for animal in list_of_all_animals:
            if animal[0] == specific_animal:
                list_of_specific_animals = np.append(list_of_specific_animals,animal,0)
    return list_of_specific_animals

def delete_specific_animals_from(list_of_all_animals,bad_animals):
    """
    delete all rows of bad_animal in provided list
    takes in a list of bad animals e.g. ['dragonfly','butterfly']
    returns list of only desired animals
    """
    all_useful_animals = list_of_all_animals
    positions_of_bad_animals = []
    for n,animal in enumerate(list_of_all_animals):
        if animal[0] in bad_animals:
            positions_of_bad_animals.append(n)
    if len(positions_of_bad_animals):
        for position in sorted(positions_of_bad_animals,reverse=True):
            # reverse is important
            # without it,list positions change as you delete items
            all_useful_animals = np.delete(all_useful_animals,(position),0)
    return all_useful_animals

emplaced_animals = delete_specific_animals_from(emplaced_animals,['dragonfly','butterfly'])

list_of_elephants = get_specific_animals_from(emplaced_animals,['elephant'])

list_of_needed_animals = get_specific_animals_from(emplaced_animals,listed_animals)
本文链接:https://www.f2er.com/3163870.html

大家都在问