简体   繁体   English

在Python中,如何比较两个列表并获取匹配的所有索引?

[英]In Python, 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. 我知道list.index()获得第一个,你可以很容易地得到最后一个。 For example: 例如:

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

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

Then I would iterate through list1 comparing to list2 and output: 然后我将遍历list1,比较list2和输出:
[0,0,0,1,1,0] , [3,4] or etc for the first iteration 第一次迭代的[0,0,0,1,1,0] , [3,4]或等
[0,0,0,0,0,1] , [6] for second [0,0,0,0,0,1] , [6]为第二
and [0,0,0,0,0,0] or [] for third [0,0,0,0,0,0][]为第三

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. 我希望以某种方式获得结果,然后我可以像这样使用它们 - 我有第三个列表可以调用list3,我想从输出的索引中获取该列表中的值。 ie list3[previousindexoutput]=list of cooresponding values list3[previousindexoutput]=list of cooresponding values

我个人开始:

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

This does not answer the question. 这不回答这个问题。 See my comment below. 请参阅下面的评论。

As a start: 作为开始:

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

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]
[([int(item1 == item2) for item2 in list2], [n for n, item2 in enumerate(list2) if item1 == item2]) for item1 in list1]
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], []]

This will give a list of lists with True/False values instead of 1/0: 这将给出一个列表,列出True / False值而不是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: 编辑:如果你使用2.5或更高版本,这应该给1和0:

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

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)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM