How to create lists of subsets with underscores from a Python list

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 to receive 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 list of subsets of just letters or 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 the question was formulated correctly. Any help in solving this difficult problem would be greatly appreciated!

+4
source share
3 answers

You can unpack the prefix and name values ​​from one call to split:

groups = {}
for animal in animals:
    prefix, name = animal.split("_")
    if prefix not in groups:
        groups[prefix] = []
    groups[prefix].append(animal)

print groups

{'A': ['A_CAT', 'A_DOG', 'A_MOUSE'], 'C': ['C_DUCK', 'C_FOX', 'C_BIRD'], 'B': ['B_FOX', ' B_DOG ',' B_MOUSE ']}

- dict :

A = groups["A"]
B = groups["B"]
C = groups["C"]

:

groups = {}
for animal in animals:
    prefix, name = animal.split("_")
    if prefix not in groups:
        groups[prefix] = []
    groups[prefix].append(name)
+2

, , , . defaultdict:

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))

:

{'A': ['A_CAT', 'A_DOG', 'A_MOUSE'], 'C': ['C_DUCK', 'C_FOX', 'C_BIRD'], 'B': ['B_FOX', 'B_DOG', 'B_MOUSE']}
+3

itertools.groupby :

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))]

:

A
# ['A_CAT', 'A_DOG', 'A_MOUSE']

B
# ['B_DOG', 'B_FOX', 'B_MOUSE']

C
# ['C_BIRD', 'C_DUCK', 'C_FOX']

, groupby.

+2

Source: https://habr.com/ru/post/1684641/


All Articles