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