简体   繁体   中英

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.

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. 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:

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

With Python 2.7 and above, you can use a dictionary comprehension :

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:

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' :

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

#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' :

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.

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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