So I have a long list of items and I need to make a new list for all the items that match. Here’s a simplified example:
Mylist = [cat, cat, dog, dog, bear, camel, camel, camel]
So I need to get to a point where:
Catlist = [cat,cat]
Doglist = [dog, dog]
Bearlist = [bear]
Camellist = [camel, camel, camel]
But the trick is the values in Mylist will change based on the dataset that is called in, so I’m never sure of what the contents are. I just have to make lists of the contents that match.
My original question was answered; however, I need to broaden the scope of my question just a bit. Let us say that I now have a matrix called animal_data which is composed of 4 columns: Animal, Xlocation, Ylocation, and Zlocation and that the first column contains the original MyList inputs and the remaining three have the gps locations of each animal in a field. How could I separate my matrix into chunks based on the type of animal? This is what I have so far:
setofthis = set(animal_data[:,0])
IDS = {
}
for one in setofthis:
ids = [one for i in animal_data[:,0] if i == one]
IDS.update({one:ids})
for one in result:
print(one, ":", IDS[one])
The inputted data set is always changing so not all the same animals will appear in the set each time and different animals may also be added to the data set.