1

I dont know why I keep falling on simple things with numpy.Maybe I just recall something wrong, what is the best way to make an array with keys and values ?

I tried:

 group_a = np.array[{"russia" : 0}, {"saudi_arabia" : 0}, {"egypt" : 0}, "uruguay" : 0}]

TypeError: 'builtin_function_or_method' object is not subscriptable

&

 group_a = np.array({"russia" : 0}, {"saudi_arabia" : 0}, {"egypt" : 0}, "uruguay" : 0})

ValueError: only 2 non-keyword arguments accepted

&

I feel confused.

2
  • Why would you want a numpy.array like this anyway? You should start by asking yourself that question, because numpy is meant to work with primitive numeric types, not objects. Commented Jun 12, 2018 at 20:57
  • That's not a simple thing. You are trying to construct an unusual array. Commented Jun 12, 2018 at 21:04

2 Answers 2

1

First the signature of np.array:

array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)

It's a function, and uses (). It takes one positional argument, which is usually a list. Additional arguments are interpreted as the keyword arguments, dtype, copy, etc.

We can make a list of dictionaries:

In [543]: alist = [{"russia" : 0}, {"saudi_arabia" : 0}, {"egypt" : 0}, {"urugua
     ...: y" : 0}]
In [544]: alist
Out[544]: [{'russia': 0}, {'saudi_arabia': 0}, {'egypt': 0}, {'uruguay': 0}]

and we can make an array from that list. Note the dtype. The array is 1d.

In [545]: arr = np.array(alist)
In [546]: arr
Out[546]: 
array([{'russia': 0}, {'saudi_arabia': 0}, {'egypt': 0}, {'uruguay': 0}],
      dtype=object)

But why aren't you making one dictionary with multiple keys? A list or array of single element dictionaries doesn't look very useful.

In [548]: adict = {"russia" : 0, "saudi_arabia" : 0, "egypt" : 0,"uruguay" : 0}
In [549]: adict
Out[549]: {'russia': 0, 'saudi_arabia': 0, 'egypt': 0, 'uruguay': 0}

Compare ways of getting the dictionary keys:

In [550]: alist[2].keys()
Out[550]: dict_keys(['egypt'])
In [551]: adict.keys()
Out[551]: dict_keys(['russia', 'saudi_arabia', 'egypt', 'uruguay'])
In [552]: [d.keys() for d in arr]
Out[552]: 
[dict_keys(['russia']),
 dict_keys(['saudi_arabia']),
 dict_keys(['egypt']),
 dict_keys(['uruguay'])]

In [554]: np.array([list(d.items()) for d in alist],object)
Out[554]: 
array([[['russia', 0]],

       [['saudi_arabia', 0]],

       [['egypt', 0]],

       [['uruguay', 0]]], dtype=object)
In [555]: _.shape
Out[555]: (4, 1, 2)    # multidimensional object array
In [556]: __[:,0,0]
Out[556]: array(['russia', 'saudi_arabia', 'egypt', 'uruguay'], dtype=object)

we can also make a structured array from adict (but also indirectly from alist):

In [559]: list(adict.items())    # produces a list of tuples
Out[559]: [('russia', 0), ('saudi_arabia', 0), ('egypt', 0), ('uruguay', 0)]

In [561]: sarr = np.array(list(adict.items()),dtype='U20,int')
In [562]: sarr
Out[562]: 
array([('russia', 0), ('saudi_arabia', 0), ('egypt', 0), ('uruguay', 0)],
      dtype=[('f0', '<U20'), ('f1', '<i8')])

Access by field. (note this is a 1d array):

In [563]: sarr['f0']
Out[563]: array(['russia', 'saudi_arabia', 'egypt', 'uruguay'], dtype='<U20')
In [564]: sarr['f1']
Out[564]: array([0, 0, 0, 0])
Sign up to request clarification or add additional context in comments.

Comments

1

np.array is a function, so you cannot subscribe it using []. In the second case you are inputting your list of dictionaries wrong, you should make a list of them then pass it to the first argument. Right now you are passing a dictionary as arguments of the function. So this should work:

np.array([{"russia" : 0}, {"saudi_arabia" : 0}, {"egypt" : 0}, {"uruguay" : 0}])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.