简体   繁体   English

您将如何找到仅包含列表列表中唯一元素的列表?

[英]How would you find a list that contains only unique elements in a list of lists?

I am trying to create a program which takes an input of a list of lists, and gives an output of lists with only distinct elements.我正在尝试创建一个程序,该程序接受列表列表的输入,并给出仅包含不同元素的列表 output。 For example, if I had this list:例如,如果我有这个列表:

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

my output should just be我的 output 应该是

[5,8,9]

because only [5,8,9] contain elements which are not found in any other list.因为只有 [5,8,9] 包含在任何其他列表中找不到的元素。

I have created a program which seems to work, but I was wondering if there is a more reliable way to get unique values.我创建了一个似乎有效的程序,但我想知道是否有更可靠的方法来获取唯一值。

viablepath=[[1,2,3,4],[1,3,6,7],[5,8,9]]
unique=[]
flattenedpath=[]
for element in viablepath:
    if element[0] not in flattenedpath:
        unique.append(element)
    if element[0] in flattenedpath:
        for list in unique:
            if element[0] in list:
                unique.remove(list)
    for item in element:
        flattenedpath.append(item)
    
print(flattenedpath)
print(unique)



enter code here

This code works by basically flattening the input list of lists and appending to unique any value that is not found in list of lists to unique.此代码的工作原理是基本上展平列表的输入列表并将列表列表中未找到的任何值附加到唯一值以使其唯一。

i have no idea if that is a reliable strategy if im working with larger data sets which includes around 50 lists within a single list.如果我要处理更大的数据集(单个列表中包含大约 50 个列表),我不知道这是否是一个可靠的策略。

Using collections.Counter and itertools.chain.from_iterable :使用collections.Counteritertools.chain.from_iterable

from collections import Counter
from itertools import chain

lists = [[1, 2, 3, 4], [1, 3, 6, 7], [5, 8, 9]]
counts = Counter(chain.from_iterable(lists))

unique = [
    element
    for element in lists
    if all(counts[e] == 1 for e in element)
]

print(unique)
# [[5, 8, 9]]

Using a python dictionary to count the frequency of each number and then check every list to see if it is unique.使用 python 字典来计算每个数字的频率,然后检查每个列表以查看它是否唯一。 Using list methods like.remove or checking if the element is in a list takes O(n) while a hashmap(python dictionary) on average takes O(1) which is much faster.使用像.remove 这样的列表方法或检查元素是否在列表中需要 O(n),而 hashmap(python 字典)平均需要 O(1),这要快得多。

def is_unique(list_of_numbers,numbers_frequency):
    for number in list_of_numbers:
        if numbers_frequency[number] > 1 :
            return False
    return True

def find_unique_lists(list_of_lists):
    numbers_frequency = {}

    for list_of_numbers in list_of_lists:
        for number in list_of_numbers:
            if number not in numbers_frequency:
                numbers_frequency[number] = 0

            numbers_frequency[number] += 1

    result = []
    for  list_of_numbers in list_of_lists:
        if is_unique(list_of_numbers,numbers_frequency):
            result.append(list_of_numbers)

    return result

input_1 = [ [1,2,3,4],
          [1,3,6,7],
          [5,8,9]  ]
expected_output1 = [[5,8,9]]

input_2 = [[1,2,3,4],
          [5,6,7,8],
          [9,10,11,12]]

expected_output2 = [[1,2,3,4],
                    [5,6,7,8],
                    [9,10,11,12]]

input_3 = [[10,13,14],
          [10,11,12],
          [8,9,10]]

expected_output3 =  []

input_4 = [[1,2,3,i] for i in range(100)]
input_4.append([100,200,300])
input_4.append([101,110,111])

expected_output4 = [[100,200,300],
                    [101,110,111]]

print(find_unique_lists(input_1) == expected_output1 )
print(find_unique_lists(input_2) == expected_output2 )
print(find_unique_lists(input_3) == expected_output3 )
print(find_unique_lists(input_4) == expected_output4 )

#output
#True
#True
#True
#True

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

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