Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create subset lists with underscores from a list Python

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!

like image 548
MichaelRSF Avatar asked Mar 09 '23 05:03

MichaelRSF


2 Answers

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']}
like image 134
Ajax1234 Avatar answered May 02 '23 10:05

Ajax1234


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.

like image 40
pylang Avatar answered May 02 '23 09:05

pylang