简体   繁体   中英

Python - How to create sublists from list of strings based on part of the string?

I saw similar questions but unfortunately I didnt found answer for my problem.

I have a list:

list = ['a_abc', 'a_xyz', 'a_foo', 'b_abc', 'b_xyz', 'b_foo']

I want to split this list into 3 based on character after underscore _. Desired output would be:

list_1 = ['a_abc', 'b_abc']
list_2 = ['a_xyz', 'b_xyz']
list_3 = ['a_foo', 'b_foo']

I would like to avoid something like:

for element in list:
   if 'abc' in element... 
   if 'xyz' in element...

because I have over 200 strings to group in this way in my use case. So code "should recognize" the same part of the string (after underscore) and group this in sublists.

Since I didnt notice similar issue any advice is highly appreciated.

You shouldn't want to do this with one or more lists, because you don't know at runtime how many there are (or, even if you know, it will be repeated code).

Instead, you can use defaultdict ; it's like a default dictionary, but handles missing value simply creating a new element with your specified factory.

In this case, defaultdict(list) means to create a dictionary with a list factory; when a key is missing, the object will create an empty list for that key.

from collections import defaultdict

l = ['a_abc', 'a_xyz', 'a_foo', 'b_abc', 'b_xyz', 'b_foo']
d = defaultdict(list)
for el in l:
    key = el.split("_")[1]
    # key = el[2:]  # use this if the format of elements is <letter>_<other_chars>
    d[key].append(el)
    
print(d)
# defaultdict(<class 'list'>, {'abc': ['a_abc', 'b_abc'], 'xyz': ['a_xyz', 'b_xyz'], 'foo': ['a_foo', 'b_foo']})
print(d["abc"])
# ['a_abc', 'b_abc']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM