首页 文章

在两个列表中找到输出相似性

提问于
浏览
-2

两个问题 . 我的代码包含两个列表

ListA = [[3, 5], [4, 4], [4, 5], [4, 6], [5, 3], [5, 4], [5, 5], [5, 6], [5, 7], [6, 4], [6, 5], [6, 6], [7, 5]]

ListB =[[0, 4], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [3, 6], [3, 7], [4, 2], [4, 3], [4, 4], [4, 5], [4, 6], [5, 2], [5, 3], [5, 4], [5, 5], [5, 6], [6, 4]]

我希望它输出两个列表之间的相似之处:

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

print(set(ListA]).intersection(ListB])) 没有用 . 还有其他方法吗?

2.我还希望它输出两个列表中找到的两个数字的每个括号的索引[0]中相似的数字 .

ListA 包含 [3,5][4,4][5,3][6,4] ListB 包含 [3,1][4,2][5,2][6,4]

由于 3,4,5,6 在每个括号的索引[0](或第一个数字)中找到,因此输出将是 .

[3,4,5,6]

我该怎么办?

5 回答

  • 2

    你可以使用这样的列表理解:

    listA = [[4, 4], [4, 5], [4, 6], [5, 4], [5, 5], [5, 6], [6, 4], [6, 5], [6, 6]]
    listB = [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [3, 2], [3, 3], [3, 4], [3, 5], [3, 6], [4, 2], [4, 3], [4, 4], [4, 5], [4, 6], [5, 2], [5, 3], [5, 4], [5, 5], [5, 6]]
    
    common = [i for i in listA if i in listB]
    

    它可能不是最快的方法,但除非您使用大型列表,否则它很好 .

  • 0

    列表不可清除,因此如果要使用 set.intersection ,则必须转换为 tuple 1):

    >>> set(map(tuple, ListA)).intersection(map(tuple, ListB))
    set([(6, 4), (5, 4), (4, 6), (4, 5), (4, 4), (5, 5), (5, 6), (3, 5), (5, 3)])
    

    并返回列表,如果你想:

    >>> sorted(map(list, set(map(tuple, ListA)).intersection(map(tuple, ListB))))
    [[3, 5], [4, 4], [4, 5], [4, 6], [5, 3], [5, 4], [5, 5], [5, 6], [6, 4]]
    

    一旦你有了这个,你就可以获得第二部分的第一个元素 set .

    >>> set(x[0] for x in _)  # _ being the previous result
    {3, 4, 5, 6}
    

    这看起来有点复杂,但使用 set 它比O(n)更快,比每个元素的整个列表中的线性搜索更快,为O(n²) .


    在不使用 set.intersection 的情况下,您还可以使用列表推导来过滤 ListB 中也在 ListA 中的那些元素(反之亦然),但同样,您应该将其他列表转换为 set ,以便查找更快(O(1)而不是O(n)),为此,你再次必须转换为 tuple . (在这种情况下,您也可以转换为 repr 并进行比较,但我尝试了两者并且 tuple 似乎要快得多 . )

    >>> setA = set(map(tuple, ListA))
    >>> [b for b in ListB if tuple(b) in setA]
    [[3, 5], [4, 4], [4, 5], [4, 6], [5, 3], [5, 4], [5, 5], [5, 6], [6, 4]]
    

    这也具有保留 ListB 中元素的原始顺序的优点,并且也更快一些 . 或者,您可以创建一个字典,将列表中的第一个元素映射到相应的第二个元素的集合,然后使用列表解析,检查该字典是否存在该元素 .

    dictA = {}
    for (x, y) in ListA:
        dictA.setdefault(x, set()).add(y)
    common = [b for b in ListB if b[0] in dictA and b[1] in dictA[b[0]]]
    

    这应该同样快,即O(n),并且可能需要更少的内存来创建所有这些集合(取决于列表中的元素) .


    1)或列表的任何其他明确的可清晰表示,但 tuple 似乎真的是唯一可行的选择 . frozenset 不关心元素的顺序,当你在交集后转换回 repreval 列表时,你真的不应该这样做 .

  • 0

    您最初的想法不起作用,因为您不能将列表设置为集合,因为它们不可清除 . 一种简单的方法是将列表转换为元组:

    ListA = [[4, 4], [4, 5], [4, 6], [5, 4], [5, 5], [5, 6], [6, 4], [6, 5], [6, 6]]
    ListB =[[0, 4], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [3, 6], [3, 7], [4, 2], [4, 3], [4, 4], [4, 5], [4, 6], [5, 2], [5, 3], [5, 4], [5, 5], [5, 6], [6, 4]]
    
    set_a = set([tuple(l) for l in ListA])
    set_b = set([tuple(l) for l in ListB])
    
    intersection = set_a.intersection(set_b)
    print(intersection)
    # {(6, 4), (5, 4), (4, 6), (5, 5), (4, 5), (5, 6), (4, 4)}
    
    # or, if you prefer a list of lists
    print([list(t) for t in intersection])
    # [[6, 4], [5, 4], [4, 6], [5, 5], [4, 5], [5, 6], [4, 4]]
    
  • 0

    您可以使用简单的嵌套循环 . 示例是here

    firstIsEqual = set()
    allEquals = []
    
    for Av1, Av2 in ListA:
      for Bv1, Bv2 in ListB:
        if Av1 == Bv1:
          firstIsEqual.add(Av1)
          if Av2 == Bv2:
            allEquals.append([Av1, Av2])
    
    print(firstIsEqual)
    print(allEquals)
    
  • 2

    第二部分有些困惑,你在找这个吗?

    ListA = [[3, 5], [4, 4], [4, 5], [4, 6], [5, 3], [5, 4], [5, 5], [5, 6], [5, 7], [6, 4], [6, 5], [6, 6], [7, 5]]
    
    ListB =[[0, 4], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [3, 6], [3, 7], [4, 2], [4, 3], [4, 4], [4, 5], [4, 6], [5, 2], [5, 3], [5, 4], [5, 5], [5, 6], [6, 4]]
    
    from itertools import groupby
    def similiraty(list_1,list_2):
        final_list = [i for i in list_1 if i in list_2]
        for a,b in groupby(final_list,lambda x:x[0]):
            print({a:list(b)})
    
    
    
    
    
    print(similiraty(ListB,ListA))
    

    输出:

    {3: [[3, 5]]}
    {4: [[4, 4], [4, 5], [4, 6]]}
    {5: [[5, 3], [5, 4], [5, 5], [5, 6]]}
    {6: [[6, 4]]}
    

    或者干脆:

    return [a for a,b in groupby(final_list,lambda x:x[0])]
    

    输出:

    [3, 4, 5, 6]
    

相关问题