Kosig
Kosig

Reputation: 201

How to compare two lists and get all indices of matches

This is probably a simple question that I am just missing but I have two lists containing strings and I want to "bounce" one, element by element, against the other returning the index of the matches. I expect there to be multiple matches and want all of the indices. I know that list.index() gets the first and you can easily get the last. For example:

list1 = ['AS144','401M','31TP01']

list2 = ['HDE342','114','M9553','AS144','AS144','401M']

Then I would iterate through list1 comparing to list2 and output:
[0,0,0,1,1,0] , [3,4] or etc for the first iteration
[0,0,0,0,0,1] , [6] for second
and [0,0,0,0,0,0] or [] for third

EDIT: Sorry for any confusion. I would like to get the results in a way such that I can then use them like this- I have a third list lets call list3 and I would like to get the values from that list in the indices that are outputed. ie list3[previousindexoutput]=list of cooresponding values

Upvotes: 10

Views: 26177

Answers (7)

Hugh Bothwell
Hugh Bothwell

Reputation: 56694

def findInstances(list1, list2):
    """For each item in list1,
    return a list of offsets to its occurences in list2
    """

    for i in list1:
        yield [pos for pos,j in enumerate(list2) if i==j]

list1 = ['AS144','401M','31TP01']
list2 = ['HDE342','114','M9553','AS144','AS144','401M']

res = list(findInstances(list1, list2))

results in

[[3, 4], [5], []]

Upvotes: 2

GreenMatt
GreenMatt

Reputation: 18590

This will give a list of lists with True/False values instead of 1/0:

matches = [ [ list1[i] == list2[j] for j in range(0, len(list2)) ] for i in range(0, len(list1)) ]

Edit: If you're using 2.5 or later, this should give 1's & 0's:

matches = [ [ 1 if list1[i] == list2[j] else 0 for j in range(0, len(list2)) ] for i in range(0, len(list1)) ]

Upvotes: 0

Andrew Clark
Andrew Clark

Reputation: 208665

This should do what you want and it can be easily turned into a generator:

>>> [[i for i in range(len(list2)) if item1 == list2[i]] for item1 in list1]
[[3, 4], [5], []]

Here is a version with a slightly different output format:

>>> [(i, j) for i in range(len(list1)) for j in range(len(list2)) if list1[i] == list2[j]]
[(0, 3), (0, 4), (1, 5)]

Upvotes: 1

ezod
ezod

Reputation: 7421

[([int(item1 == item2) for item2 in list2], [n for n, item2 in enumerate(list2) if item1 == item2]) for item1 in list1]

Upvotes: 1

Dennis Benzinger
Dennis Benzinger

Reputation: 783

This does not answer the question. See my comment below.

As a start:

list(i[0] == i[1] for i in zip(list1, list2))

Upvotes: 3

porgarmingduod
porgarmingduod

Reputation: 7888

Personally I'd start with:

matches = [item for item in list1 if item in list2]

Upvotes: 10

Ned Batchelder
Ned Batchelder

Reputation: 376012

I'm not sure how you want these packaged up, but this does the work:

def matches(lst, value):
    return [l == value for l in lst]

all_matches = [matches(list2, v) for l in list1]

Upvotes: 1

Related Questions