简体   繁体   中英

Position of indices of a list with respect to another list in Python

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]

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]

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.

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])

Assuming A have no duplicates and B s 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM