从列表比较和绘图中获取位置数据 [英] Get location data from list comparison and plot

查看:49
本文介绍了从列表比较和绘图中获取位置数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my code, the user inputs a text file which is saved as the variable "emplaced_animals_data." This variable has four columns (Animal ID, X location, Y location, and Z location) and the number of rows varies depending on which text file is uploaded. I then have another list (listed_animals) which contains animals that we want to gather location data about from the emplaced_animals_data. So far, I have created a new variable for each item in the listed_animals list. I want to be able to compare each of these new variables to my emplaced_items_data Animal ID column and store their appropriate locations without having to explicitly call "Animal1, Animal2, etc." Here is the code I currently have and what is being outputted:

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视觉

The data being used in the emplaced_animals_data list can be found here: emplaced_animals_data visual

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

推荐答案

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

See below code, I generated my own data with random numbers to mimic your data. This is just a slight modification to use numpy lists from your other question:

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)

这篇关于从列表比较和绘图中获取位置数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆