I have a list called animals,
animals = ["B_FOX", "A_CAT", "A_DOG", "A_MOUSE",
"B_DOG", "B_MOUSE", "C_DUCK", "C_FOX", "C_BIRD"]
and would like the following outputs:
A = ["A_CAT", "A_DOG", "A_MOUSE"]
B = ["B_DOG", "B_MOUSE", "B_FOX"]
C = ["C_DUCK", "C_FOX", "C_BIRD"]
I can only get a subset list of only the letters or the animals like this:
[species.split("_",1)[1] for species in animals]
['FOX', 'CAT', 'DOG', 'MOUSE', 'DOG', 'MOUSE', 'DUCK', 'FOX', 'BIRD']
[letters.split("_",1)[0] for letters in animals]
['B', 'A', 'A', 'A', 'B', 'B', 'C', 'C', 'C']
Not sure if I've worded the question correctly. Any help in solving this tricky problem would be greatly appreciated!
You could build separate lists, one for each initial letter, however, that would be tricky if you have many letters. You can use a defaultdict
instead:
from collections import defaultdict
d = defaultdict(list)
animals = ["B_FOX", "A_CAT", "A_DOG", "A_MOUSE",
"B_DOG", "B_MOUSE", "C_DUCK", "C_FOX", "C_BIRD"]
for animal in animals:
d[animal[0]].append(animal)
print(dict(d))
Output:
{'A': ['A_CAT', 'A_DOG', 'A_MOUSE'], 'C': ['C_DUCK', 'C_FOX', 'C_BIRD'], 'B': ['B_FOX', 'B_DOG', 'B_MOUSE']}
Try an itertools.groupby
according to the first letter:
import operator as op
import itertools as it
animals = [
"B_FOX", "A_CAT", "A_DOG", "A_MOUSE",
"B_DOG", "B_MOUSE", "C_DUCK", "C_FOX", "C_BIRD"
]
A, B, C = [list(g) for _, g in it.groupby(sorted(animals), key=op.itemgetter(0))]
Outputs:
A
# ['A_CAT', 'A_DOG', 'A_MOUSE']
B
# ['B_DOG', 'B_FOX', 'B_MOUSE']
C
# ['C_BIRD', 'C_DUCK', 'C_FOX']
Here is a post on how groupby
works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With