ShanZhengYang ShanZhengYang - 1 year ago 67
Python Question

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

Answer Source

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)


>>> import pprint
>>> pprint.pprint(d)
 'DS': 'f:115.5',
 '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)


>>> pprint.pprint(d)
{'CB': ['E:OWTOWTW'],
 'DS': ['f:115.5'],
 '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']}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download