简体   繁体   English

在Python中,如何获取两个列表的交集,并保留交集的顺序?

[英]In Python, how can I get the intersection of two lists, preserving the order of the intersection?

I have a list of lists ("sublists") and I want to see if the same sequence of any unspecified length occurs in more than one sublist. 我有一个列表列表(“子列表”),我想看看是否有多个未指定长度的相同序列出现在多个子列表中。 To clarify, the order of items must be preserved - I do not want the intersection of each sublist as a set. 为了明确起见,必须保留项目的顺序-我不希望每个子列表的交集都作为集合。 There must be at least 2 items that match sequentially. 必须至少有2个项目相匹配。 Please see example below. 请参见下面的示例。

Input: 输入:

someList = [[0,1,3,4,3,7,2],[2,3,4,3],[0,3,4,3,7,3]]

Desired Output: (will be printed to file but don't worry about this detail) 所需的输出:(将打印到文件中,但不必担心此细节)

sublist0_sublist1 = [3,4,3] #intersection of 1st and 2nd sublists sublist0_sublist1 = [3,4,3] #第一个和第二个子列表的交集

sublist0_sublist2 = [3,4,3,7] #intersection of 1st and 3rd sublists sublist0_sublist2 = [3,4,3,7] #第一个和第三个子列表的交集

sublist1_sublist2 = [3,4,3] #intersection of 2nd and 3rd sublists sublist1_sublist2 = [3,4,3] #第二和第三子列表的交集

Whipped this up for you (including your comment that equal-length maximum sublists should all be returned in a list): 为您重提了这一点(包括您的评论,即等长最大子列表都应在列表中返回):

def sublists(list1, list2):
    subs = []
    for i in range(len(list1)-1):
        for j in range(len(list2)-1):
            if list1[i]==list2[j] and list1[i+1]==list2[j+1]:
                m = i+2
                n = j+2
                while m<len(list1) and n<len(list2) and list1[m]==list2[n]:
                    m += 1
                    n += 1
                subs.append(list1[i:m])
    return subs

def max_sublists(list1, list2):
    subls = sublists(list1, list2)
    if len(subls)==0:
        return []
    else:
        max_len = max(len(subl) for subl in subls)
        return [subl for subl in subls if len(subl)==max_len]

This works allright for these cases: 这些情况适用于以下情况:

In [10]: max_sublists([0,1,3,4,3,7,2],[0,3,4,3,7,3])
Out[10]: [[3, 4, 3, 7]]
In [11]: max_sublists([0,1,2,3,0,1,3,5,2],[1,2,3,4,5,1,3,5,3,7,3])
Out[11]: [[1, 2, 3], [1, 3, 5]]

It's not pretty though, nor is it really fast. 虽然不是很漂亮,但也不是很快。

You only have to figure out how to compare every sublist in your original list of sublists, but that should be easy. 您只需要弄清楚如何比较原始子列表中的每个子列表,但这应该很容易。

[Edit: I fixed a bug and prevented your error from occurring.] [编辑:我修复了一个错误,并防止发生您的错误。]

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

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