简体   繁体   English

具有列表推导的Python字典值赋值

[英]Python Dictionary value assignment with list comprehension

I would like to take a python dictionary of lists, convert the lists to numpy arrays, and restore them in the dictionary using list comprehension. 我想拿一个列表的python字典,将列表转换为numpy数组,并使用list comprehension在字典中恢复它们。

For example, if I had a dictionary 例如,如果我有一本字典

myDict = {'A':[1,2,3,4], 'B':[5,6,7,8], 'C':'str', 'D':'str'}

I wish to convert the lists under keys A and B to numpy arrays, but leave the other parts of the dictionary untouched. 我希望将键A和B下的列表转换为numpy数组,但保持字典的其他部分不变。 Resulting in 导致

myDict = {'A':array[1,2,3,4], 'B':array[5,6,7,8], 'C':'str', 'D':'str'}

I can do this with a for loop: 我可以使用for循环执行此操作:

import numpy as np

for key in myDict:
    if key not in ('C', 'D'):
        myDict[key] = np.array(myDict[key])

But is it possible to do this with list comprehension? 但是有可能用列表理解来做到这一点吗? Something like 就像是

[myDict[key] = np.array(myDict[key]) for key in myDict if key not in ('C', 'D')]

Or indeed what is the fastest most efficient way to achieve this for a large dictionaries of long lists. 或者实际上,对于长列表的大型词典来说,实现此目的的最快最有效的方法是什么。 Thanks, labjunky 谢谢,labjunky

With Python 2.7 and above, you can use a dictionary comprehension : 使用Python 2.7及更高版本,您可以使用字典理解

myDict = {'A':[1,2,3,4], 'B':[5,6,7,8], 'C':'str', 'D':'str'}
myDict = {key:np.array(val) if key not in {'C', 'D'} else val for key, val in myDict.iteritems()}

If you're below version 2.7 (and hence don't have dictionary comprehensions), you can do: 如果您的版本低于2.7(因此没有字典理解),您可以:

myDict = {'A':[1,2,3,4], 'B':[5,6,7,8], 'C':'str', 'D':'str'}
dict((key, np.array(val) if key not in {'C', 'D'} else val for key, val in myDict.iteritems())

To change all items except 'C' and 'D' : 要更改除'C''D'以外'C'所有项目:

>>> myDict = {'A':[1,2,3,4], 'B':[5,6,7,8], 'C':'str', 'D':'str'}
>>> ignore = {'C', 'D'}
>>> new_dict = {k : v if k in ignore else np.array(v) for k,v in myDict.iteritems()}

the above dict-comprehension returns a new dictionary, to modify the original dict, try: 上面的dict-comprehension返回一个新的字典,修改原来的字典,试试:

#myDict.viewkeys() - ignore --> set(['A', 'B'])
for key in myDict.viewkeys() - ignore:
    myDict[key] = np.array(myDict[key])

or if you only want to change 'A' and 'B' : 或者如果你只想改变'A''B'

for key in {'A', 'B'}:
    myDict[key] = np.array(myDict[key])

To

take a python dictionary of lists, convert the lists to numpy arrays, and restore them in the dictionary using list comprehension. 获取列表的python字典,将列表转换为numpy数组,并使用list comprehension在字典中恢复它们。

I would do: 我会做:

myDict = {'A':[1,2,3,4], 'B':[5,6,7,8], 'C':'str', 'D':'str'}

def modifier(item):
    if type(item) == list:
        return np.array(item)
    else:
        return item

mod_myDict = {key: modifier(myDict[key]) for key in myDict.keys()}

The function then sets the restrictions you require in this case changing all lists into arrays. 然后,该函数设置在这种情况下所需的限制,将所有列表更改为数组。 This returns: 返回:

{'A': array([1, 2, 3, 4]), 'B': array([5, 6, 7, 8]), 'C': 'str', 'D': 'str'}

Note: I believe this should be made shorter by using conditions in the if statement but I can't seem to figure it, something like 注意:我相信这应该通过在if语句中使用条件来缩短,但我似乎无法想象它,类似于

mod_myDict = {key: np.array(myDict[key]) if type(myDict[key])==list else key: myDict[key] for key in myDict.keys()}

That however, raises an error. 然而,这引起了一个错误。 Perhaps some more intelligent person than I knows why! 或许比我知道的更聪明的人为什么!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM