首页 > 解决方案 > 从列表比较和绘图中获取位置数据

问题描述

在我的代码中,用户输入了一个文本文件,该文件保存为变量“emplaced_animals_data”。此变量有四列(动物 ID、X 位置、Y 位置和 Z 位置),行数因上传的文本文件而异。然后我有另一个列表 (listed_animals),其中包含我们想要从 emplaced_animals_data 收集位置数据的动物。到目前为止,我已经为listed_animals 列表中的每个项目创建了一个新变量。我希望能够将这些新变量中的每一个与我的 emplaced_items_data Animal 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, 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 列表中使用的数据可以在这里找到: emplaced_animals_data 视觉

我的目标是用不同的符号绘制每只动物的位置,但是因为listed_animals 列表中可能并不总是有相同的动物或相同数量的动物,所以我不能明确地调用每只动物。那么关于如何进行迭代的任何想法?

标签: pythonlistloopsdata-manipulationgeneric-programming

解决方案


见下面的代码,我用随机数生成了我自己的数据来模仿你的数据。这只是使用其他问题中的 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)

推荐阅读