Finding the indices of the values of one list in another list

Karnivaurus

I have two Python lists of integers: x, and y. All elements of x appear somewhere in y, and only once. For each element of x, I want to know the index of the corresponding value in y. I then want to set these indices to a list z.

The code below works as I have just described. However, it seems a little clumsy for a task which I suspect may have a more elegant solution in just a couple of lines or so. Therefore, my question is, what is the least number of lines in which the following code can be rewritten?

z = [0 for i in range(len(x))]
for i, j in enumerate(x):
    for k, l in enumerate(y):
        if j==l:
            z[i] = k
            break
csiu

Here is one way to do it:

z = [y.index(i) for i in x]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Finding elements from a list included in another one

From Dev

Transforming list to dict with list items as keys and list indices as values in python

From Dev

indexing and finding values in list of namedtuples

From Dev

Remove values from one list if they appear in another

From Dev

Inserting values in one list into another list by index

From Dev

Create a list with items from another list at indices specified in a third list

From Dev

Getting indices of elements that are in another list in numpy

From Dev

Swapping indices with values in a Python list?

From Dev

Scala sort one list according to the values in another one

From Dev

Finding indices of matches of one array in another array

From Dev

Python: matching values from one list to the sequence of values in another list

From Dev

Finding a set of indices that maps the rows of one NumPy ndarray to another

From Dev

LINQ query for finding one item in list AND verifying list does not contain another item

From Dev

use one list to replace values in another

From Dev

How to Order one list by the index values stored in another list

From Dev

Finding the Indices of multiple items in a list, using a list

From Dev

Substituting the values from list of lists by the values of another list based on indices in python

From Dev

Remove parts of a list based on another list of indices

From Dev

retrieve HashMap list into another list , one key having many values

From Dev

Remove values from one list if they appear in another

From Dev

Inserting values in one list into another list by index

From Dev

Getting indices of elements that are in another list in numpy

From Dev

use one list to replace values in another

From Dev

Summing up elements from one list based on indices in another list

From Dev

Referencing the values in one array list to values in another

From Dev

accessing elements of a tensor with another list of indices in tensorflow

From Dev

Finding indices of items from a list in another list even if they repeat

From Dev

Subtracts values one list from another in javascript

From Dev

Return values of a list for specific list of indices from another list

Related Related

  1. 1

    Finding elements from a list included in another one

  2. 2

    Transforming list to dict with list items as keys and list indices as values in python

  3. 3

    indexing and finding values in list of namedtuples

  4. 4

    Remove values from one list if they appear in another

  5. 5

    Inserting values in one list into another list by index

  6. 6

    Create a list with items from another list at indices specified in a third list

  7. 7

    Getting indices of elements that are in another list in numpy

  8. 8

    Swapping indices with values in a Python list?

  9. 9

    Scala sort one list according to the values in another one

  10. 10

    Finding indices of matches of one array in another array

  11. 11

    Python: matching values from one list to the sequence of values in another list

  12. 12

    Finding a set of indices that maps the rows of one NumPy ndarray to another

  13. 13

    LINQ query for finding one item in list AND verifying list does not contain another item

  14. 14

    use one list to replace values in another

  15. 15

    How to Order one list by the index values stored in another list

  16. 16

    Finding the Indices of multiple items in a list, using a list

  17. 17

    Substituting the values from list of lists by the values of another list based on indices in python

  18. 18

    Remove parts of a list based on another list of indices

  19. 19

    retrieve HashMap list into another list , one key having many values

  20. 20

    Remove values from one list if they appear in another

  21. 21

    Inserting values in one list into another list by index

  22. 22

    Getting indices of elements that are in another list in numpy

  23. 23

    use one list to replace values in another

  24. 24

    Summing up elements from one list based on indices in another list

  25. 25

    Referencing the values in one array list to values in another

  26. 26

    accessing elements of a tensor with another list of indices in tensorflow

  27. 27

    Finding indices of items from a list in another list even if they repeat

  28. 28

    Subtracts values one list from another in javascript

  29. 29

    Return values of a list for specific list of indices from another list

HotTag

Archive