-1

How do I do the following without a for loop?

import numpy as np

l = np.array([[1, 3], 1, np.nan, [3, 53, 13], ['225gg2g']], dtype=object)

loc = [1, 2]

for i in loc:
    l[i] = ['wgwg', 23, 'g']
Divakar
  • 218,885
  • 19
  • 262
  • 358
gino
  • 1
  • 1
  • Is [this](https://stackoverflow.com/questions/8373079/numpy-function-to-set-elements-of-array-to-a-value-given-a-list-of-indices) essentially what you want? – Cory Kramer Oct 31 '17 at 19:13
  • 1
    @CoryKramer the complication here is that the `np.ndarray` is of `dtype` object, and presumably, fancy-index based assignment `l[[1,2]] = some_list` is going to not treat `some_list` as a scalar value. Of course, this all raises the question, **why is the OP using a `numpy.ndarray` to begin with**? – juanpa.arrivillaga Oct 31 '17 at 19:15
  • l[loc] = ['wgwg', 23, 'g'] does not work. it returns array([[1, 3], 'wgwg', 23, [3, 53, 13], ['225gg2g']], dtype=object) instead of array([[1, 3], ['wgwg', 23, 'g'], ['wgwg', 23, 'g'], [3, 53, 13], ['225gg2g']], dtype=object) – gino Oct 31 '17 at 19:22
  • 1
    @gino why in the world are you even using `numpy` here? It sounds like you want a regular `list`, although, it actually sounds like you need to rethink the way you've organized your data altogether... – juanpa.arrivillaga Oct 31 '17 at 19:35

1 Answers1

1
In [424]: l = np.array([[1, 3], 1, np.nan, [3, 53, 13], ['225gg2g']], dtype=object)
In [425]: loc = [1,2]
In [426]: l[loc]
Out[426]: array([1, nan], dtype=object)
In [427]: l[loc] = ['wgwg',23,'g']
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-427-53c5987a9ac6> in <module>()
----> 1 l[loc] = ['wgwg',23,'g']

ValueError: cannot copy sequence with size 3 to array axis with dimension 2

It's trying to broadcast the 3 item list (converted to an array?) into a 2 item slot.

We can wrap the list in another list, so it is now a single item list:

In [428]: l[loc] = [['wgwg',23,'g']]
In [429]: l
Out[429]: 
array([list([1, 3]), list(['wgwg', 23, 'g']), list(['wgwg', 23, 'g']),
   list([3, 53, 13]), list(['225gg2g'])], dtype=object)

Were as if we copy it one by one we get:

In [432]: l[loc[0]] = ['wgwg',23,'g']
In [433]: l[loc[1]] = ['wgwg',23,'g']
In [434]: l
Out[434]: 
array([list([1, 3]), list(['wgwg', 23, 'g']), list(['wgwg', 23, 'g']),
       list([3, 53, 13]), list(['225gg2g'])], dtype=object)

There isn't an easy to way to get around this 3=>2 mapping issue.

But why do you need to do this? It looks suspicious to me.

edit

It works I first create a 0d object array:

In [444]: item = np.empty([], object)
In [445]: item.shape
Out[445]: ()
In [446]: item[()] = ['wgwg',23,'g']
In [447]: item
Out[447]: array(['wgwg', 23, 'g'], dtype=object)
In [448]: l[loc] = item
In [449]: l
Out[449]: 
array([list([1, 3]), list(['wgwg', 23, 'g']), list(['wgwg', 23, 'g']),
       list([3, 53, 13]), list(['225gg2g'])], dtype=object)
hpaulj
  • 221,503
  • 14
  • 230
  • 353