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

Python 从列表比较和绘图中获取位置数据,python,list,loops,data-manipulation,generic-programming,Python,List,Loops,Data Manipulation,Generic Programming,在我的代码中,用户输入一个文本文件,该文件保存为变量“emplaced_animals_data”。该变量有四列(动物ID、X位置、Y位置和Z位置),行数根据上载的文本文件而不同。然后我有另一个列表(列出的动物),其中包含我们想要从安置的动物数据中收集位置数据的动物。到目前为止,我已经为列表中的每个项目创建了一个新变量。我希望能够将这些新变量中的每一个与我安置的_items_data Animal ID列进行比较,并存储它们的适当位置,而无需显式调用“Animal1、Animal2等”。以下是我

在我的代码中,用户输入一个文本文件,该文件保存为变量“emplaced_animals_data”。该变量有四列(动物ID、X位置、Y位置和Z位置),行数根据上载的文本文件而不同。然后我有另一个列表(列出的动物),其中包含我们想要从安置的动物数据中收集位置数据的动物。到目前为止,我已经为列表中的每个项目创建了一个新变量。我希望能够将这些新变量中的每一个与我安置的_items_data Animal ID列进行比较,并存储它们的适当位置,而无需显式调用“Animal1、Animal2等”。以下是我当前拥有的代码和正在输出的内容:

列出的动物=['cat','dog','bear','camel','elephant']
动物1_Xloc=[]
动物1_Yloc=[]
动物1_Zloc=[]
对于i,枚举中的值(列出的值):
对于范围内的j(0,len(安置的动物数据)):
exec(“动物%s=值”%(i))
如果Animal1==安置的动物数据[j,0]:#不想显式调用
Animal1_Xloc=np.append(Animal1_Xloc,安置的动物数据[j,1])
Animal1_-Yloc=np.append(Animal1_-Yloc,安置的动物数据[j,2])
Animal1_Zloc=np.append(Animal1_Zloc,安置的动物数据[j,3])
印刷品(动物1)
打印('X位置:',动物1_Xloc)
打印('Y位置:',动画)
打印('Z位置:',动物1_Zloc)
狗
X位置:['1''2''3''4''1''2''3''4''1''2''3''4''1''2''3''4''1''2'
'3' '4']
Y位置:['3''12''10''8''3''12''10''8''3''12''10''8''3''12''10''8'
'3' '12' '10' '8']
Z位置:['9''8''1''1''9''8''1''1''9''8''1''1''9''8''1''1''1''9''8'
'1' '1']
安置动物数据列表中使用的数据可在以下位置找到:


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

请参见下面的代码,我用随机数生成了自己的数据,以模拟您的数据。这只是对您的另一个问题的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)

每当我听到有人问“如何为每个变量创建一个新变量…”时,可能会出现重复。我想:那个人需要学习字典:@jez我知道。。。我发现字典非常混乱,我不确定它是否适用于我在这种情况下试图做的事情。@Sayse也许?页面上的大部分内容都指向使用字典,这是我不想做/不知道如何做的事情……或者,对于@Sayse来说,每个键(动物)都有一个值(位置坐标列表或元组(x,y,z))