Home > other >  Use numpy. Numpy array mapping vectorize method (through the dictionary keys: values) times wrong
Use numpy. Numpy array mapping vectorize method (through the dictionary keys: values) times wrong

Time:10-11

Have a numpy array and a dictionary D K, want to pass the keys of the dictionary D: array K values relationship on a map, to generate an array of the same size as the K, the range for the values in the dictionary D, wanted to use numpy vectorize method of vector operation, when the values of the dictionary is a list of the list is used when no problem, but as the list is an error, tip: setting an array element with a sequence. More surprisingly, if K is not the first data dictionary keys, is not an error, want to ask everybody to solve,


The following error: ValueError: setting an array element with a sequence.
Expect [list ([3, 4]) list ([1, 2])]
 
The import numpy as np
D={' A ': [1, 2],' B ': [3, 4]}
K=np. Array ([' B ', 'A'])
V=np. Vectorize (D.g et) (K)
Print (V)


The following code is normal, only K first data in the array with the keys
 
The import numpy as np
D={' A ': [1, 2],' B ': [3, 4]}
K=np. Array ([' C ', 'A'])
V=np. Vectorize (D.g et) (K)
Print (V)

CodePudding user response:

Although no understand vectorize problem, but let it can use the map mapping
 
V=list (map (D.g et, K))
Print (V)

CodePudding user response:

reference 1st floor 尐 dongdong response:
although didn't understand vectorize question, but let it can use the map mapping
 
V=list (map (D.g et, K))
Print (V)

When K is 1 d, can use the map, but not a K for 2 d, tip TypeError: unhashable type: 'numpy. Ndarray', of course with np. Flatten () will be converted to 1 K d, then reshape can realize, I mainly want to use the np vectorize (), some more concise, but the title is shown in error
  • Related