How to convert a dictionary to a NumPy array in Python

3 Answers

0 votes
import numpy as np

d = {"a": 1, "b": 2, "c": 3}

arr = np.array(list(d.values()))
print(arr)


'''
run:

[1 2 3]

'''

 



answered Jan 19 by avibootz
0 votes
import numpy as np

d = {"a": 1, "b": 2, "c": 3}

arr = np.array(list(d.items()))
print(arr)


'''
run:

[['a' '1']
 ['b' '2']
 ['c' '3']]

'''

 



answered Jan 19 by avibootz
0 votes
import numpy as np

d = {'Ages': [30, 16, 63, 57], 
     'Years': [2025, 2026, 2027, 2028]}

arr = np.asarray(list(d.values()))

print(arr)
print(type(arr))


'''
run:

[[  30   16   63   57]
 [2025 2026 2027 2028]]
<class 'numpy.ndarray'>

'''

 



answered Jan 19 by avibootz
...