Reputation: 3
If I have the following array:
T = array([[list([2])],
[list([])],
[list([0])]], dtype=object)
All I want to happen is when I type T[0]
, I want the the index [2]
to be returned. At present I get array([list([2])], dtype=object.
I don't want the list, the round brackets, the other text, I just want [2]
.
I have a more complex case I'm wanting to try this out on but have created a simple example first to see if the concept works and is where I'm having trouble. I have the following function in which you specify a set to search, and provide the set that has the elements in ti that you're looking for. The function then returns the index in the main set of where the values were found.
find = lambda searchList, elem: [[i for i, x in enumerate(searchList) if x == e] for e in elem]
My main set that I wanted to look into is composed of two string arrays, then joined together into a 3 x 2 array.
A = np.array([['A'], ['B'],['C'], ['D']])` # main set 1
array([['A'],
['B'],
['C'],
['D']],
dtype='<U1')
D = np.array([['E'], ['F'],['G'], ['H']]) # main set 2
array([['E'],
['F'],
['G'],
['H']],
dtype='<U1')
F = np.column_stack((A,D)) # Combined Array - The main set in which to look through using the find function.
array([['A', 'E'],
['B', 'F'],
['C', 'G'],
['D', 'H']],
dtype='<U1')
I have done exactly the same with a made up search set (composed of two smaller sets)
C = np.array([['C'], ['D'], ['A']])
E = np.array([['G'], ['D'], ['E']])
G = np.ndarray.tolist(F)
I wanted to combine these into a single array and have them side by side, 3 rows, 2 columns.
H = np.column_stack((C,E))
array([['C', 'G'],
['D', 'D'],
['A', 'E']],
dtype='<U1')
When I apply the find function, it does indeed return to me the indexes of where the elements are in the main array.
T = find(G, I)
array([list([2]), list([]), list([0])], dtype=object)
Now, I have created another array, which just has the number 1, 2, 3 and 4 in it
B = np.array([[1],[2],[3],[4]])
I wanted to use the indexes returned to me in the search function i.e.
[2], [] (blank) and [0]
Any pull the numbers from array B and store them in a separate array
i.e. I wanted L = [3],[],[1]
Using: L = np.empty([len(I),1])
for j in range(len(H)):
L[j] = B[T[j]]
However I get the error:
L[j] = B[T[j]]
ValueError: could not broadcast input array from shape (0,1) into shape (1)
Upvotes: 0
Views: 78
Reputation: 3591
A solution that works with the exact data given is T[0][0]; T[0] returns array([list([2])], and taking element zero of that returns [2] (and taking element zero of that returns 2). However, it's not clear what application this is for (and referring the [2] as an "index" raises red flags). It might be better to give a larger context, and get suggestions on how to accomplish your larger goal. For instance, if you store this as a Series or DataFrame, you can store [2] as a name, and then access it that way instead of drilling down several layers in the array.
Upvotes: -1
Reputation: 77837
You need to properly index the element. Here are the various levels of your nested lists:
>>> import numpy as np
>>> T = np.array([[list([2])],
... [list([])],
... [list([0])]], dtype=object)
>>> T
array([[[2]],
[[]],
[[0]]], dtype=object)
>>> T[0]
array([[2]], dtype=object)
>>> T[0][0]
[2]
>>> T[0][0][0]
2
See how that works?
If you want T[0] to be 2, then you need to make 2
the first element of the list, rather than a list containing the list containing 2. For instance:
>>> S = np.array([2, None, 0])
>>> S
array([2, None, 0], dtype=object)
>>> S[0]
2
Upvotes: 2