问题
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']
The data being used in the emplaced_animals_data list can be found here: emplaced_animals_data visual
My goal is to plot each animals' locations with a different symbol, but because the listed_animals list may not always have the same animals or the same number of animals in it I can't call each animal explicitly. So any ideas on how I could make this iterative?
回答1:
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)
来源:https://stackoverflow.com/questions/58700735/get-location-data-from-list-comparison-and-plot