Home > Software design >  Position of indices of a list with respect to another list in Python
Position of indices of a list with respect to another list in Python

Time:06-21

I am trying to locate the positions of indices of list B: (0,2),(2,1) with respect to list A. But there is an error. The desired output is attached.

import numpy 
A=[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
B=[(0,2),(2,1)]
C=B.index(A)
print("C =",C)

The error is

<module>
    C=B.index(A)

ValueError: [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)] is not in list

The desired output is

[3,8]

CodePudding user response:

To get the indices of the wanted pairs, you can do:

a=[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
b=[(0,2),(2,1)]
c=[a.index(b_item) for b_item in b]
print("C =", c)

This will print [2,7] (indices starting from 0). Alternatively, if you want indices starting from 1 as output (result [3,8]):

a=[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
b=[(0,2),(2,1)]
c=[a.index(b_item) 1 for b_item in b]
print("C =", c)

Note that this will result in an error if the pair is not in list a. You can work with try and except ValueError if you want to avoid errors.

CodePudding user response:

I think what you're trying to do can be solved with a list comprehension as follows:

import numpy 
A=[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
B=[(0,2),(2,1)]
C= [A.index(element) for element in B]
print("C =",C)

That will return:

[2,7]

If you want it to return your desired output, just do:

C= [A.index(element) 1 for element in B]

CodePudding user response:

Because you tag . You need to check each row of B with each row in A then use numpy.all and numpy.any. (but you need to consider in python, Index start from zero, if you want [3,8]. you need to 1 result.)

>>> np.argwhere((A==B[:,None]).all(-1).any(0)).ravel()
array([2, 7])

Explanation:

>>> A = np.asarray(A)
>>> B = np.asarray(B)
>>> A == B[:,None]
array([[[ True, False],
        [ True, False],
        [ True,  True],
        [False, False],
        [False, False],
        [False,  True],
        [False, False],
        [False, False],
        [False,  True]],

       [[False, False],
        [False,  True],
        [False, False],
        [False, False],
        [False,  True],
        [False, False],
        [ True, False],
        [ True,  True],
        [ True, False]]])

>>> (A==B[:,None]).all(axis=-1)
array([[False, False,  True, False, False, False, False, False, False],
       [False, False, False, False, False, False, False,  True, False]])

>>> (A==B[:,None]).all(axis=-1).any(axis=0) <- you want index of this array that have `True` value
array([False, False,  True, False, False, False, False,  True, False])

>>> np.argwhere((A==B[:,None]).all(axis=-1).any(axis=0))
array([[2],
       [7]])

>>> np.argwhere((A==B[:,None]).all(axis=-1).any(axis=0)).ravel()
array([2, 7])

CodePudding user response:

Ok, Olli's answer is obviously correct, but I feel there are a few misundertandings in your post that need an explanation.

C=B.index(A) asks for the position of A in B, which I believe is quite the opposite of what you want. Hence the error, A is not in B. But even A.index(B) would give an error, because once more B as a whole is not in A.

What you want to know is the position in A of each single element of B, or to be more precise of the first occurence in A of each element of B. So you need to iterate over each element of B and find its position in A. This can be done in a few different ways, but the logic will always be the same

CodePudding user response:

Assuming A have no duplicates and Bs are contained in the A, you can do this based on this answer:

(np.array(A)[:, None] == np.array(B)).all(-1).argmax(0)   1
# [3, 8]

I have used 1 because indexing in Numpy arrays are starting from 0. I recommend using numpy equivalent methods instead of loops because they will be much better than loops in terms of performance when working on large arrays.

  • Related