1

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.

td_python
  • 25
  • 5
  • Welcome to StackOverflow! Please [edit](https://stackoverflow.com/posts/58610419/edit) your post to include the code you've already written in your solution attempts, as well as any other relevant info about your dataset or other factors. – Das_Geek Oct 29 '19 at 16:06

3 Answers3

0

Using individual variables for each list is not the way to go — especially since you don't know ahead of time what the data contains. You should use a data structure like a dictionary to make the data easy to work with and keep it in one place.

You can use itertools.groupby to turn your list into a dictionary of lists. The keys will be the animal and the values will be the list of animals. You will need to make sure the list is sorted so the identical animals are grouped together:

from itertools import groupby

Mylist = ['cat', 'dog', 'bear', 'camel', 'camel','cat', 'camel','dog']
animals = {k:list(g) for k, g in groupby(sorted(Mylist))}

Result animals:

{'bear': ['bear'],
 'camel': ['camel', 'camel', 'camel'],
 'cat': ['cat', 'cat'],
 'dog': ['dog', 'dog']}

From here you can get your individual lists by simply indexing into the dictionary:

print(animals['cat']) # ['cat', 'cat']
Mark
  • 90,562
  • 7
  • 108
  • 148
0
Mylist = ['cat', 'cat', 'dog', 'dog', 'bear', 'camel', 'camel', 'camel']
cats = []
dogs = []
camels = []

for item in Mylist:
    if item == 'cat':
        cats += [item]
    if item == 'dog':
        dogs += [item]
    if item == 'camel':
        camels += [item]

print(cats)
print(dogs)
print(camels)
0

You could also use this simple piece of code with list comprehension

Mylist = ["cat", "cat", "dog", "dog", "bear", "camel", "camel", "camel"]
setofthis = set(Mylist)

result = {
}
for one in setofthis:
    res = [one for i in Mylist if i == one]
    result.update({one: res})

for one in result:
    print(one, " : ",result[one])

from this u will get something like this

dog  :  ['dog', 'dog']
bear  :  ['bear']
cat  :  ['cat', 'cat']
camel  :  ['camel', 'camel', 'camel']
Chikko
  • 81
  • 1
  • 7
  • Thank you! This was very helpful, but now I need to make it just a little more complicated to really get to the heart of my problem. Let us say that Mylist 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 large dataset into smaller sets based on the type of animal and containing the animal and its location? – td_python Oct 30 '19 at 18:15
  • without a specific input its hard to help you. In which data structure are the information animal,Xlocation,Ylocation,Zlocation currently stored? list of dicts? – Chikko Oct 31 '19 at 15:13
  • if you have a list of dict like this: l = [ { 'animal' : 'cat', 'x': 2, 'y':4, 'z':1},{'animal...}] - list comprehension is still the easiest way, just try this: result = [record for record in data if record["animal"] == "cat"]. This gives you a list that contains only 'cat' records. Greetings – Chikko Oct 31 '19 at 15:37
  • The data is an inputted text file from the user and is in array form with four columns and a changing number of rows. The rows change based on what file is inputted. I don’t know if I can use a dictionary because the type of animal that could be in the dataset is upwards of 200 items. I really just need to write something that will search my first column for animals that are the same and group each appearance of the animal with its location but everything I have tried isn’t working. (I guess I’m not the best programmer ha.) Thanks again for the support! – td_python Nov 01 '19 at 15:16
  • I think I did a much better job explaining my question here: https://stackoverflow.com/questions/58663133/how-do-i-manipulate-a-large-data-set-into-smaller-sets-based-on-the-type-of-obje if you'd like to take a look. – td_python Nov 01 '19 at 17:08