find index of element corresponding to another list

CIsForCookies

I have a list of strings: ls = ['a','b','c'] and another one, with larger strings, guaranteed to include one and only one strings from ls: ls2 = ['1298a', 'eebbbd', 'qcqcq321']".

How can I find, for a given string from ls2, what is the index of the corresponding string from ls?

I can use:

for s in ls:
    for ss in ls2:
        if s in ss:
            print (s,ss,ls.index(s))

a 1298a 0
b eebbbd 1
c qcqcq321 2

but it there something nicer?

EDIT (hope it clarifies):

The actual case I'm working on has a bigger 1st list, and a smaller 2nd:

ls  = ['apo','b','c','d25','egg','f','g']
ls2 = ['apoip21', 'oiujohuid25']

and I want to get the result 0,3 because the 1st item in ls2 has the 1st item from ls, while the 2nd in ls2 has the 4th in ls

Mad Physicist

It doesn't look like you can get away from O(m * n * p) complexity (where m = len(ls), n = len(ls2), p = max(map(len, ls2))) without further information about your data. You can definitely reduce your current loop from O(m2 * n * p) by keeping track of the current index using enumerate. Also, don't forget about early termination:

for string in ls2:
    for index, key in enumerate(ls):
        if key in string:
            print(key, string, index)
            break

Notice that I swapped the inner and outer loop to make the break work properly: you definitely want to check each element of ls2, but only the minimum number of elements in ls.

Here are some timings I accumulated on the different O(m * n * p) solutions presented here. Thanks to @thierry-lathuille for the test data:

ls = ['g', 'j', 'z', 'a', 'rr', 'ttt', 'b', 'c', 'd', 'f']
ls2 = ['1298a', 'eebbb', 'qcqcq321', 'mlkjmd', 'dùmlk', 'lof',
       'erreee', 'bmw', 'ottt', 'jllll', 'lla' ]

def with_table():
    table = {key: index for index, key in enumerate(ls)}
    result = {}
    for string in ls2:
        for key in ls:
            if key in string:
                result[string] = table[key]
    return result

def with_enumerate():
    result = {}
    for string in ls2:
        for index, key in enumerate(ls):
            if key in string:
                result[string] = index
                break
    return result

def with_dict_comp():
    return {string: index for string in ls2 for index, key in enumerate(ls) if key in string}

def with_itertools():
    result = {}
    for (index, key), string in itertools.product(enumerate(ls), ls2):
        if key in string:
            result[string] = index
    return result

%timeit with_table()
4.89 µs ± 61.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit with_enumerate()
5.27 µs ± 66.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit with_dict_comp()
6.9 µs ± 83.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit with_itertools()
17.5 ns ± 0.193 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

As it turns out, creating a lookup table for the indices is slightly faster than computing them on the fly with enumerate.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find the minimum value of a list and print the corresponding index from another list

From Dev

How to find the index of last element on list and add it to another

From Dev

Finding list of index corresponding to list of element of a dataframe in Python

From Dev

python - Fastest way to find index of an element in list by comparing to elements of another list

From Dev

find if any element of list is in another list

From Dev

find index of array element in another array javascript

From Dev

find index of array element in another array javascript

From Dev

Find index of element in sub-sub-list

From Dev

Find and print index of element in a list (string)

From Dev

how to check whether a list element is in another list but also at the same index

From Dev

Replacing selected elements in a list with the corresponding element from another list, very basic beginning programmer style

From Java

How to find if a list contains any element of another list in sqlite

From Dev

Find index of smallest element in an array not in another array (Matlab)

From Dev

R - Find index of element from one dataframe and place in another

From Dev

Find maximum of each row in a numpy array and the corresponding element in another array of the same size

From Dev

In Perl, how can find the smallest value in a list and keeping the index corresponding to that value?

From Dev

Check if element of vector is in corresponding element of list

From Dev

Check if element of vector is in corresponding element of list

From Dev

How to find the index of next matching element in list using LINQ

From Dev

How to find index of last element in list matching predicate?

From Dev

How to find index of element in list that starts with a given string

From Dev

How to iterate and sum each element in a list with another to find a number?

From Dev

Find the index corresponding to the max value of a numpy array

From Dev

Find list in another list

From Dev

ElementTree, Python - find element with sub-element containing certain text and add another sub-element to a list

From Dev

ElementTree, Python - find element with sub-element containing certain text and add another sub-element to a list

From Dev

Removing an element from a list and a corresponding value

From Dev

What is the most efficient way to find the index of an element in a list, given only an element of a sublist (Python)

From Dev

How to find an index of an element from a list<string>, an element contains a specific part

Related Related

  1. 1

    Find the minimum value of a list and print the corresponding index from another list

  2. 2

    How to find the index of last element on list and add it to another

  3. 3

    Finding list of index corresponding to list of element of a dataframe in Python

  4. 4

    python - Fastest way to find index of an element in list by comparing to elements of another list

  5. 5

    find if any element of list is in another list

  6. 6

    find index of array element in another array javascript

  7. 7

    find index of array element in another array javascript

  8. 8

    Find index of element in sub-sub-list

  9. 9

    Find and print index of element in a list (string)

  10. 10

    how to check whether a list element is in another list but also at the same index

  11. 11

    Replacing selected elements in a list with the corresponding element from another list, very basic beginning programmer style

  12. 12

    How to find if a list contains any element of another list in sqlite

  13. 13

    Find index of smallest element in an array not in another array (Matlab)

  14. 14

    R - Find index of element from one dataframe and place in another

  15. 15

    Find maximum of each row in a numpy array and the corresponding element in another array of the same size

  16. 16

    In Perl, how can find the smallest value in a list and keeping the index corresponding to that value?

  17. 17

    Check if element of vector is in corresponding element of list

  18. 18

    Check if element of vector is in corresponding element of list

  19. 19

    How to find the index of next matching element in list using LINQ

  20. 20

    How to find index of last element in list matching predicate?

  21. 21

    How to find index of element in list that starts with a given string

  22. 22

    How to iterate and sum each element in a list with another to find a number?

  23. 23

    Find the index corresponding to the max value of a numpy array

  24. 24

    Find list in another list

  25. 25

    ElementTree, Python - find element with sub-element containing certain text and add another sub-element to a list

  26. 26

    ElementTree, Python - find element with sub-element containing certain text and add another sub-element to a list

  27. 27

    Removing an element from a list and a corresponding value

  28. 28

    What is the most efficient way to find the index of an element in a list, given only an element of a sublist (Python)

  29. 29

    How to find an index of an element from a list<string>, an element contains a specific part

HotTag

Archive