Indices of cross-referenced lists

maelstromscientist

I'm cross-referencing two lists to find which items coincide between the two lists. The first list orig is 32 items in size, and I'm cross-referencing it with a much-larger list sdss which is 112,000 items in size. So far this is what I've got:

for i in range(0,len(orig),1):
    if orig[i] in sdss:
        print('\n %s' % (orig[i]))

This gives me the items that are the same between the two lists, however, how would I efficiently return the indices (or location) of cross-referenced items inside of the sdss list (the larger list)?

EDIT: I guess I should've been clearer. I am actually cross-referencing two arrays with ints, not strings.

LearnerEarner

If the order is not of importance you can use set intersection to find the unique common elements and list comprehension to get the index and element as tuple

[(sdss.index(common_element),common_element) for common_element in set(orig) & set(sdss)]

Note that "index" raises ValueError if the value is not found in the list but in this case the value WILL exist in sdss. So, no need to worry about nonexistent elements throwing errors.

You can also use numpy.intersect1d

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related