0

I have a numpy array that has lists inside. this is my numpy array structure

array([list(['記', 'hk', '超抵', '玩', '轉台', '優惠', '無限', '任用']),
       list(['学生', '个人', '兼职', '援交', '加']), list(['轉', '台大', '優惠']), ...,
       list(['af', 'ia']), list(['交換', 'sourc']),
       list(['美食', 'cf', 'asm', '幾分'])], dtype=object)

The i've used

np.where(Arr=="ia")

but i didn't get any index return. this is my output

(array([], dtype=int64),)

How can i access the element index like normal numpy array? thanks in advance.

  • Do you need the array to stay an array of lists instead of a 2d array? If so - numpy won't be able to find this automatically for you, you would have to search for it manually – ChocolateBear Jul 08 '21 at 13:49
  • even i convert it to array([array(["string])]) it still doesn't work for me. –  Jul 08 '21 at 13:51
  • when i convert it to 2d array it get something like this:array([array(['記', 'hk', '超抵', '玩', '轉台', '優惠', '無限', '任用'], dtype=' –  Jul 08 '21 at 13:54
  • And I don't need my array to stay as array of list what i want i just to acess the index of the element inside. –  Jul 08 '21 at 13:58
  • Since the lists inside that array vary in length, you have, effectively a list of lists. `numpy` methods won't help you. – hpaulj Jul 08 '21 at 14:58
  • That `where` just returns the location of the `True` values in `Arr=="ia"`. That is probably a 1d array all `False`, because none of the lists match string. They may contain that string, but doesn't test for that. – hpaulj Jul 08 '21 at 15:01
  • Do you need the index of the list containing the element or the index of the element in the list or both? – LGrementieri Jul 08 '21 at 15:27
  • both but with the index of the list that contain that element also fine for me –  Jul 08 '21 at 15:28

2 Answers2

0

NumPy only works with arrays and list of the same length.

The best you can do is make all the lists of the same size appending None elements and then use NumPy functions. The drawback of using this approach is that, if there is a list that is much larger than the others, the filled array will use a lot memory to save dummy None values.

Taking inspiration from this answer, you can use the following code

a = np.array([list(['記', 'hk', '超抵', '玩', '轉台', '優惠', '無限', '任用']),
       list(['学生', '个人', '兼职', '援交', '加']), list(['轉', '台大', '優惠']),
       list(['af', 'ia']), list(['交換', 'sourc']),
       list(['美食', 'cf', 'asm', '幾分'])], dtype=object)
max_length = max(len(l) for l in a)
filled_array = np.array([l + [None] * (max_length-len(l)) for l in a])
print(np.where(filled_array=="ia"))
LGrementieri
  • 760
  • 4
  • 12
0
In [213]: Arr=np.array([list(['記', 'hk', '超抵', '玩', '轉台', '優惠', '無限', '任用']),
     ...:        list(['学生', '个人', '兼职', '援交', '加']), list(['轉', '台大', '優惠'])
     ...: ,
     ...:        list(['af', 'ia']), list(['交換', 'sourc']),
     ...:        list(['美食', 'cf', 'asm', '幾分'])], dtype=object)
In [214]: Arr
Out[214]: 
array([list(['記', 'hk', '超抵', '玩', '轉台', '優惠', '無限', '任用']),
       list(['学生', '个人', '兼职', '援交', '加']), list(['轉', '台大', '優惠']),
       list(['af', 'ia']), list(['交換', 'sourc']),
       list(['美食', 'cf', 'asm', '幾分'])], dtype=object)

Using regular Python list methods:

In [215]: for i,l in enumerate(Arr):
     ...:     print(i, 'ia' in l)
     ...: 
0 False
1 False
2 False
3 True
4 False
5 False

In [216]: for i,l in enumerate(Arr):
     ...:     if 'ia' in l:
     ...:         print(i,l.index('ia'))
     ...: 
3 1

An object dtype array is basically a list, with few, if any, advantages. numpy also does not have much of its own string methods.

hpaulj
  • 221,503
  • 14
  • 230
  • 353