开发者

Split dictionary of lists into single lists

开发者 https://www.devze.com 2023-03-23 12:07 出处:网络
How can I split a dictionary of two lists into two different lists? The structure of the dictionary is following:

How can I split a dictionary of two lists into two different lists?

The structure of the dictionary is following:

{'key1': ['P开发者_StackOverflowTRG0097',
         'CPOG0893',
         'MMUG0444',
         'BTAG0783'],
'key2': ['CPOG0893',
         'MMUG0444',
         'PPYG0539',
         'BTAG0083']}


That's how we unroll:

>>> d = {'key1': ['PTRG0097', 'CPOG0893', 'MMUG0444', 'BTAG0783'], 'key2': ['CPOG0893', 'MMUG0444', 'PPYG0539', 'BTAG0083']}
>>> l1, l2 = d.values() # or this: d['key1'], d['key2']
>>> l1
['PTRG0097', 'CPOG0893', 'MMUG0444', 'BTAG0783']
>>> l2
['CPOG0893', 'MMUG0444', 'PPYG0539', 'BTAG0083']


How about a['key1'] and a['key2']

or

a.values()[0] and a.values()[1]?


key1,key2 = yourdict.values()

should do it.

key1 and key2 now being lists containing the values in the corresponding dictionary list.


Well you cna try something like :

l1=l2=[]
for key, val in dict.items()
    l1.append(key)
    l2.append(val)

as Each dict entry has key and val you can use items() method of dict

or l = [ for val in d.values() ]

Regards

0

精彩评论

暂无评论...
验证码 换一张
取 消