简体   繁体   English

python - 列表中列表之间的公共列表

[英]python - Common lists among lists in a list

I need to be able to find the first common list (which is a list of coordinates in this case) between a variable amount of lists. 我需要能够在可变数量的列表之间找到第一个公共列表(在这种情况下是一个坐标列表)。

ie this list 即这个清单

>>> [[[1,2],[3,4],[6,7]],[[3,4],[5,9],[8,3],[4,2]],[[3,4],[9,9]]]

should return 应该回来

>>> [3,4]

If easier, I can work with a list of all common lists(coordinates) between the lists that contain the coordinates. 如果更简单,我可以使用包含坐标的列表之间的所有常用列表(坐标)的列表。

I can't use sets or dictionaries because lists are not hashable(i think?). 我不能使用集合或字典,因为列表不可清除(我认为?)。

Correct, list objects are not hashable because they are mutable. 正确, list对象不可清除,因为它们是可变的。 tuple objects are hashable (provided that all their elements are hashable). tuple对象是可清除的(前提是它们的所有元素都是可清除的)。 Since your innermost lists are all just integers, that provides a wonderful opportunity to work around the non-hashableness of lists: 由于你的最里面的列表都只是整数,这提供了一个很好的机会来解决列表的不可删除性:

>>> lists = [[[1,2],[3,4],[6,7]],[[3,4],[5,9],[8,3],[4,2]],[[3,4],[9,9]]]
>>> sets = [set(tuple(x) for x in y) for y in lists]
>>> set.intersection(*sets)
set([(3, 4)])

Here I give you a set which contains tuples of the coordinates which are present in all the sublists. 在这里,我给你一个包含所有子列表中存在的坐标元组的集合。 To get a list of list like you started with: 要获得您开始使用的列表列表:

[list(x) for x in set.intersection(*sets)]

does the trick. 诀窍。

To address the concern by @wim, if you really want a reference to the first element in the intersection (where first is defined by being first in lists[0] ), the easiest way is probably like this: 为了解决由@wim的关注,如果你真的想要的第一个元素在路口的参考 (其中first是由暂时先定义lists[0]最简单的方法大概是这样的:

#... Stuff as before
intersection = set.intersection(*sets)
reference_to_first = next( (x for x in lists[0] if tuple(x) in intersection), None ) 

This will return None if the intersection is empty. 如果交叉点为空,这将返回None

If you are looking for the first child list that is common amongst all parent lists, the following will work. 如果您要查找所有父列表中常见的第一个子列表,则以下内容将起作用。

def first_common(lst):
    first = lst[0]
    rest = lst[1:]
    for x in first:
        if all(x in r for r in rest):
            return x

Solution with recursive function. 具有递归功能的解决方案。 :) :)

This gets first duplicated element. 这是第一个重复的元素。

def get_duplicated_element(array):
    global result, checked_elements
    checked_elements = []
    result = -1
    def array_recursive_check(array):
        global result, checked_elements
        if result != -1: return
        for i in array:
            if type(i) == list:
                if i in checked_elements:
                    result = i
                    return
                checked_elements.append(i)
                array_recursive_check(i)
    array_recursive_check(array)
    return result

get_duplicated_element([[[1,2],[3,4],[6,7]],[[3,4],[5,9],[8,3],[4,2]],[[3,4],[9,9]]])
[3, 4]

you can achieve this with a list comprehension: 你可以用列表理解来实现这个目的:

>>> l = [[[1,2],[3,4],[6,7]],[[3,4],[5,9],[8,3],[4,2]],[[3,4],[9,9]]]
>>> lcombined =  sum(l, [])
>>> [k[0] for k in [(i,lcombined.count(i)) for i in lcombined] if k[1] > 1][0]
[3, 4]

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

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