简体   繁体   中英

How to convert a list of strings to a dict of lists where keys start with given substring

I have a list like this:

list = ['ID - ISO', 'CATA - CIT', 'CATA - CIT2', 'ID - ISO6', 'CATA - CIT', 'CATA - CIT2', 'CATA - CIT6', 'CATA - CIT8']

I would like to create a dictionary like this

dict = {'ID - ISO': ['CATA - CIT', 'CATA - CIT2'], 
        'ID - ISO6': ['CATA - CIT', 'CATA - CIT2', 'CATA - CIT6', 'CATA - CIT8']}

So I developed this code but when I add the values, it is not working: the values are the same for all the ids.

with open("/data/myfile") as openfile:
        for line in openfile:
        for single_line in line.split('\\'):
            if line.startswith("ID"):
                 Dict[line] = None
            elif line.startswith("CATA"):
                for li in Dict:
                    Dict[li]=line
print Dict

You could use itertools.groupby() :

from itertools import groupby
lst = ['ID - ISO', 'CATA - CIT', 'CATA - CIT2','ID - ISO6', 'CATA - CIT', 'CATA - CIT2', 'CATA - CIT6', 'CATA - CIT8']

result = {}
for k,v in groupby(lst, lambda x: x.startswith('ID')):
    if k:
        key = next(v)
    else:
        result[key] = list(v)

print(result)

Which would yield

{
 'ID - ISO': ['CATA - CIT', 'CATA - CIT2'], 
 'ID - ISO6': ['CATA - CIT', 'CATA - CIT2', 'CATA - CIT6', 'CATA - CIT8']
}

Additionally, do not call your variables after builtin types ( list , dict , tuple , etc.) - you're effectively shadowing the functionality if you do.

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