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

durbachit

This answer works very well for finding indices of items from a list in another list, but the problem with it is, it only gives them once. However, I would like my list of indices to have the same length as the searched for list. Here is an example:

thelist = ['A','B','C','D','E'] # the list whose indices I want
Mylist = ['B','C','B','E'] # my list of values that I am searching in the other list
ilist = [i for i, x in enumerate(thelist) if any(thing in x for thing in Mylist)]

With this solution, ilist = [1,2,4] but what I want is ilist = [1,2,1,4] so that len(ilist) = len(Mylist). It leaves out the index that has already been found, but if my items repeat in the list, it will not give me the duplicates.

user94559
thelist = ['A','B','C','D','E']
Mylist = ['B','C','B','E']
ilist = [thelist.index(x) for x in Mylist]

print(ilist)  # [1, 2, 1, 4]

Basically, "for each element of Mylist, get its position in thelist."

This assumes that every element in Mylist exists in thelist. If the element occurs in thelist more than once, it takes the first location.

UPDATE

For substrings:

thelist = ['A','boB','C','D','E']
Mylist = ['B','C','B','E']
ilist = [next(i for i, y in enumerate(thelist) if x in y) for x in Mylist]

print(ilist)  # [1, 2, 1, 4]

UPDATE 2

Here's a version that does substrings in the other direction using the example in the comments below:

thelist = ['A','B','C','D','E']
Mylist = ['Boo','Cup','Bee','Eerr','Cool','Aah']

ilist = [next(i for i, y in enumerate(thelist) if y in x) for x in Mylist]

print(ilist)  # [1, 2, 1, 4, 2, 0]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

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

From Dev

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

From Dev

Finding combinations of vectors from pairs of list items

From Dev

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

From Dev

Finding ContiguousCount of items in list?

From Dev

Finding ContiguousCount of items in list?

From Dev

Excluding items from a list that exist in another list

From Dev

How to create list of items from another list

From Dev

How to repeat a list of items from a vector in hiccup?

From Java

Finding elements from a list included in another one

From Dev

Remove elements from a list, using another list as indices

From Dev

Changing value of variable in list using indices from another list

From Dev

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

From Dev

How to filter one list of items from another list of items?

From Dev

Click items in an ng-repeat to fill another list

From Dev

Subset items in list of list based on indices

From Dev

Finding a matching list of items in SQL

From Dev

Finding a range in excel for a list of items

From Dev

finding list of minimum items in a vector

From Dev

Remove list of items from another file in bash

From Dev

find lists that start with items from another list

From Dev

Copy list of items to another list

From Dev

Remove parts of a list based on another list of indices

From Dev

Checking if List contains all items from another list

From Dev

Remove items from a List which are in another List by a specific property?

From Dev

Using LINQ to remove items from a list that do not apear in another list

From Dev

Remove Items from sub list matching another sub list with Linq

From Dev

removing items from list that exist in another list based on an ID member

Related Related

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

    Finding combinations of vectors from pairs of list items

  5. 5

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

  6. 6

    Finding ContiguousCount of items in list?

  7. 7

    Finding ContiguousCount of items in list?

  8. 8

    Excluding items from a list that exist in another list

  9. 9

    How to create list of items from another list

  10. 10

    How to repeat a list of items from a vector in hiccup?

  11. 11

    Finding elements from a list included in another one

  12. 12

    Remove elements from a list, using another list as indices

  13. 13

    Changing value of variable in list using indices from another list

  14. 14

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

  15. 15

    How to filter one list of items from another list of items?

  16. 16

    Click items in an ng-repeat to fill another list

  17. 17

    Subset items in list of list based on indices

  18. 18

    Finding a matching list of items in SQL

  19. 19

    Finding a range in excel for a list of items

  20. 20

    finding list of minimum items in a vector

  21. 21

    Remove list of items from another file in bash

  22. 22

    find lists that start with items from another list

  23. 23

    Copy list of items to another list

  24. 24

    Remove parts of a list based on another list of indices

  25. 25

    Checking if List contains all items from another list

  26. 26

    Remove items from a List which are in another List by a specific property?

  27. 27

    Using LINQ to remove items from a list that do not apear in another list

  28. 28

    Remove Items from sub list matching another sub list with Linq

  29. 29

    removing items from list that exist in another list based on an ID member

HotTag

Archive