[英]Finding distinct elements in a list
给定这样的列表,其中第一列是ID,第二列是字符串,
x = [ [1, ["cat","dog"]],
[2, ["dog", "mouse", "elephant"]],
[3, ["mouse", "giraffe"]] ]
我想知道一种有效地将所有不同元素归为另一个列表的方法。
之所以出现我的问题,是因为我必须满足复杂性要求。
O(UCK),其中U是列表中项目的数量,C是任何动物中字符的最大数量,K是列表中动物的最大数量。
输出示例:
[ ["cat"],
["dog"],
["mouse"],
["elephant"],
["giraffe"] ]
我的解决方案使用字典来做到这一点:
distinctList = []
distinctDict = {}
for item in x:
for animal in item[1]:
if animal not in distinctDict:
distinctList.append(animal)
distinctDict[animal] = 1
但是,这样做的复杂度将变为O(UKN),其中N是字典中的项目数。 此复杂度大于所需的复杂度。
您可以使用以下设置的理解力来做到这一点:
uniques = {animal for row in data for animal in row[1]}
data = [[1, ["cat", "dog"]],
[2, ["dog", "mouse", "elephant"]],
[3, ["mouse", "giraffe"]]]
uniques = {animal for row in data for animal in row[1]}
print(uniques)
{'cat', 'giraffe', 'mouse', 'dog', 'elephant'}
这将返回一个嵌套列表,就像您的示例输出是一个嵌套列表一样。
#!python2
x = [[1, ["cat", "dog"]], [2, ["dog", "mouse", "elephant"]], [3, ["mouse", "giraffe"]]]
new_lst = []
for sublst in x:
for subsublst in sublst[1]:
if not any(subsublst in sublst for sublst in new_lst):
new_lst.append([subsublst]) # nested list
# new_lst.append(subsublst) # a list of strings
print new_lst
'''
[['cat'], ['dog'], ['mouse'], ['elephant'], ['giraffe']]
'''
In [126]: data = [[1, ["cat", "dog"]],
...: [2, ["dog", "mouse", "elephant"]],
...: [3, ["mouse", "giraffe"]]]
In [127]: [[x] for x in {animal for row in data for animal in row[1]}]
Out[127]: [['giraffe'], ['mouse'], ['elephant'], ['cat'], ['dog']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.