Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I only parse/split this list with multiple colons in each element? Create dictionary

I have the following Python list:

list1 = ['EW:G:B<<LADHFSSFAFFF', 'CB:E:OWTOWTW', 'PP:E:A,A<F<AF', 'GR:A:OUO-1-XXX-EGD:forthyFive:1:HMJeCXX:7', 'SX:F:-111', 'DS:f:115.5', 'MW:AA:0', 'MA:A:0XT:i:0', 'EY:EE:KJERWEWERKJWE']

I would like to take the entries of this list and create a dictionary of key-values pairs that looks like

dictionary_list1 = {'EW':'G:B<<LADHFSSFAFFF', 'CB':'E:OWTOWTW', 'PP':'E:A,A<F<AF', 'GR':'A:OUO-1-XXX-EGD:forthyFive:1:HMJeCXX:7', 'SX':'F:-111', 'DS':'f:115.5', 'MW':'AA:0', 'MA':'A:0XT:i:0', 'EW':'EE:KJERWEWERKJWE'}

How does one parse/split the list above list1 to do this? My first instinct was to try try1 = list1.split(":"), but then I think it is impossible to retrieve the "key" for this list, as there are multiple colons :

What is the most pythonic way to do this?

like image 479
ShanZhengYang Avatar asked Mar 11 '23 23:03

ShanZhengYang


1 Answers

You can specify a maximum number of times to split with the second argument to split.

list1 = ['EW:G:B<<LADHFSSFAFFF', 'CB:E:OWTOWTW', 'PP:E:A,A<F<AF', 'GR:A:OUO-1-XXX-EGD:forthyFive:1:HMJeCXX:7', 'SX:F:-111', 'DS:f:115.5', 'MW:AA:0', 'MA:A:0XT:i:0', 'EW:EE:KJERWEWERKJWE']
d = dict(item.split(':', 1) for item in list1)

Result:

>>> import pprint
>>> pprint.pprint(d)
{'CB': 'E:OWTOWTW',
 'DS': 'f:115.5',
 'EW': 'EE:KJERWEWERKJWE',
 'GR': 'A:OUO-1-XXX-EGD:forthyFive:1:HMJeCXX:7',
 'MA': 'A:0XT:i:0',
 'MW': 'AA:0',
 'PP': 'E:A,A<F<AF',
 'SX': 'F:-111'}

If you'd like to keep track of values for non-unique keys, like 'EW:G:B<<LADHFSSFAFFF' and 'EW:EE:KJERWEWERKJWE', you could add keys to a collections.defaultdict:

import collections
d = collections.defaultdict(list)
for item in list1:
    k,v = item.split(':', 1)
    d[k].append(v)

Result:

>>> pprint.pprint(d)
{'CB': ['E:OWTOWTW'],
 'DS': ['f:115.5'],
 'EW': ['G:B<<LADHFSSFAFFF', 'EE:KJERWEWERKJWE'],
 'GR': ['A:OUO-1-XXX-EGD:forthyFive:1:HMJeCXX:7'],
 'MA': ['A:0XT:i:0'],
 'MW': ['AA:0'],
 'PP': ['E:A,A<F<AF'],
 'SX': ['F:-111']}
like image 143
TigerhawkT3 Avatar answered Apr 26 '23 14:04

TigerhawkT3