从列表字典到两个相同长度的列表

时间:2021-07-09 14:01:14

Consider the following dict:

考虑以下dict:

{'key1': ['PTRG0097',
         'CPOG0893',
         'MMUG0444',
         'BTAG0783'],
'key2': ['CPOG0893',
         'MMUG0444',
         'PPYG0539',
         'BTAG0083']}

I would like to convert it to two lists or tuples:

我想将其转换为两个列表或元组:

keys   = ['key1', 'key1', 'key1', 'key1', 'key2', 'key2', 'key2', 'key2']
values = ['PTRG0097', 'CPOG0893', 'MMUG0444', 'BTAG0783', 'CPOG0893', 'MMUG0444', 'PPYG0539', 'BTAG0083']

There is probably an easy way to do this without looping in Python 3, but some of the solutions I found (e.g. Split dictionary of lists into single lists) don't quite expand the dict this way.

如果没有在Python 3中循环,可能有一种简单的方法可以做到这一点,但是我发现的一些解决方案(例如将列表的列表拆分成单个列表)并没有以这种方式扩展dict。

4 个解决方案

#1


3  

This is how one would do it without explicit looping and assuming Python3.

如果没有显式循环并假设Python3,人们就会这样做。

>> list(zip(*[(k, v) for k, vs in d.items() for v in vs]))
[('key2', 'key2', 'key2', 'key2', 'key1', 'key1', 'key1', 'key1'),
 ('CPOG0893',
  'MMUG0444',
  'PPYG0539',
  'BTAG0083',
  'PTRG0097',
  'CPOG0893',
  'MMUG0444',
  'BTAG0783')]

The initial call to list is merely needed to print the iterator out. You don't need it if you want to save the values in variables:

只需要打开迭代器就可以初始调用list。如果要将值保存在变量中,则不需要它:

[keys, values] = zip(*[(k, v) for k, vs in d.items() for v in vs])

#2


2  

This is not possible without looping, but you can do it without an explicit loop:

如果没有循环,这是不可能的,但是你可以在没有显式循环的情况下完成:

from itertools import *

d = {'key1': ['PTRG0097',
              'CPOG0893',
              'MMUG0444',
              'BTAG0783'],
     'key2': ['CPOG0893',
              'MMUG0444',
              'PPYG0539',
              'BTAG0083']}

def expand(k, vs):
    return product([k], vs)

print(list(zip(*chain.from_iterable(starmap(expand, d.items())))))

In practice, you would of course go with musically_ut's answer, and not with this unreadable nonsense.

在实践中,你当然会选择musically_ut的答案,而不是这个难以理解的废话。

#3


1  

You could get a list of tuples of the keys and values this way:

您可以通过这种方式获取键和值的元组列表:

key_values = [(k, v) for k, vs in a.items() for v in vs]

and then from the list get a list of the keys and the values if this is more valuable:

然后从列表中获取键和值的列表,如果这更有价值:

keys = [i[0] for i in key_values]
values = [i[1] for i in key_values]

#4


1  

keys = ['key1' for x in d['key1']] + ['key2' for x in d['key2']]
values = d['key1'] + d['key2']

Although, I suppose there's might be a siimpler version of the 'keys' line that doesn't use list-comprehensions.

虽然,我想可能有一个简单的'keys'行版本不使用list-comprehensions。

#1


3  

This is how one would do it without explicit looping and assuming Python3.

如果没有显式循环并假设Python3,人们就会这样做。

>> list(zip(*[(k, v) for k, vs in d.items() for v in vs]))
[('key2', 'key2', 'key2', 'key2', 'key1', 'key1', 'key1', 'key1'),
 ('CPOG0893',
  'MMUG0444',
  'PPYG0539',
  'BTAG0083',
  'PTRG0097',
  'CPOG0893',
  'MMUG0444',
  'BTAG0783')]

The initial call to list is merely needed to print the iterator out. You don't need it if you want to save the values in variables:

只需要打开迭代器就可以初始调用list。如果要将值保存在变量中,则不需要它:

[keys, values] = zip(*[(k, v) for k, vs in d.items() for v in vs])

#2


2  

This is not possible without looping, but you can do it without an explicit loop:

如果没有循环,这是不可能的,但是你可以在没有显式循环的情况下完成:

from itertools import *

d = {'key1': ['PTRG0097',
              'CPOG0893',
              'MMUG0444',
              'BTAG0783'],
     'key2': ['CPOG0893',
              'MMUG0444',
              'PPYG0539',
              'BTAG0083']}

def expand(k, vs):
    return product([k], vs)

print(list(zip(*chain.from_iterable(starmap(expand, d.items())))))

In practice, you would of course go with musically_ut's answer, and not with this unreadable nonsense.

在实践中,你当然会选择musically_ut的答案,而不是这个难以理解的废话。

#3


1  

You could get a list of tuples of the keys and values this way:

您可以通过这种方式获取键和值的元组列表:

key_values = [(k, v) for k, vs in a.items() for v in vs]

and then from the list get a list of the keys and the values if this is more valuable:

然后从列表中获取键和值的列表,如果这更有价值:

keys = [i[0] for i in key_values]
values = [i[1] for i in key_values]

#4


1  

keys = ['key1' for x in d['key1']] + ['key2' for x in d['key2']]
values = d['key1'] + d['key2']

Although, I suppose there's might be a siimpler version of the 'keys' line that doesn't use list-comprehensions.

虽然,我想可能有一个简单的'keys'行版本不使用list-comprehensions。