繁体   English   中英

从多个列表和列表列表中获取唯一对象。 然后使用所有列表中的唯一对象创建一个新列表

[英]Get unique objects from multiple lists and lists of lists. Then create a new list with unique objects from all the lists

我的数据集混合了列表和列表列表。 所以我需要从列表或列表列表中获取唯一的对象,但找不到任何成功。 输入数据集如下:

[[1, 2, 39], [1, 2, 39], [3], [[3], [4, 14, 63, 65], [66, 68, 82, 94]], [[5] , 8, 31, 34], [36, 37, 42, 44], [55]], [6, 91], [[7, 35, 60, 71], [73, 83, 95, 98]] , [[5, 8, 31, 34], [36, 37, 42, 44], [55]], [9, 72], [[10, 22, 30, 32], [38, 51, 56] , 87], [89, 92]], [11], [12], [13, 90], [[4, 14, 63, 65], [66, 68, 82, 94]]]

输出要求:

[[1, 2, 39], [3], [4, 14, 63, 65], [66, 68, 82, 94], [5, 8, 31, 34], [36, 37, 42, 44, [55], [6, 91], [7, 35, 60, 71], [73, 83, 95, 98], [9, 72], [10, 22, 30, 32], [ 38, 51, 56, 87], [89, 92], [11], [12], [13, 90], [66, 68, 82, 94]]

abc=[]
for x in pool:
    if x not in abc:
        abc.append(x)

我不明白如何选择由列表和列表列表组成的列表的不同对象。 任何帮助将不胜感激。

我咨询了多个来源,但没有解决我的问题。 其中少数如下:

列表中的唯一列表

https://www.geeksforgeeks.org/python-get-unique-values-list/

从多个列表创建唯一的对象列表

您可以使用生成器将列表展平,然后遍历生成的值并添加到结果中(如果尚未看到)。 就像是:

def flatten(l):
    for item in l:
        if isinstance(item[0], list):
            yield from flatten(item)
        else:
            yield item

seen = set()
unique = [] 

for l in flatten(l):
    if tuple(l) in seen:
        continue
    seen.add(tuple(l))
    unique.append(l)

print(unique)

结果

[[1, 2, 39],
 [3],
 [4, 14, 63, 65],
 [66, 68, 82, 94],
 [5, 8, 31, 34],
 [36, 37, 42, 44],
 [55],
 [6, 91],
 [7, 35, 60, 71],
 [73, 83, 95, 98],
 [9, 72],
 [10, 22, 30, 32],
 [38, 51, 56, 87],
 [89, 92],
 [11],
 [12],
 [13, 90]]

假设您的列表设置为名为 my_list 的变量。 您可以使用普通索引访问此列表的第一个范围内的任何对象。 my_list = [[1, 2, 39], [1, 2, 39], [3], [[3], [4, 14, 63, 65], [66, 68, 82, 94]], [[5, 8, 31, 34], [36, 37, 42, 44], [55]], [6, 91], [[7, 35, 60, 71], [73, 83, 95, 98]], [[5, 8, 31, 34], [36, 37, 42, 44], [55]], [9, 72], [[10, 22, 30, 32], [38, 51, 56, 87], [89, 92]], [11], [12], [13, 90], [[4, 14, 63, 65], [66, 68, 82, 94]]] print(my_list[2])

上面的代码将打印 my_list 的第二个索引,它恰好是一个只包含数字 3 的列表

要访问列表中的列表,请指定下一个索引。 my_list[3]这个对象包含 3 个列表。 [3], [4,14,63,65], and [66,68,82,94]

要选择这 3 个列表中的第二个列表, print(my_list[3][1])首先我们选择一个包含 3 个列表的索引[3] ,然后在该列表[1]上指定另一个索引,这将为您提供[4,14,63,65]要更深入地使用另一个索引并从my_list[3][1]的列表中检索对象,请添加另一个索引。 14 位于此列表的 [1] 索引处,因此要访问它是my_list[3][1][1]

您需要迭代数据集中的每个元素以检查它是否为 type = class "List"。 如果是这样,需要检查进一步的元素。 这是您可以参考的示例代码

result = []
for list_item in list1:
 for element in list_item:
    if not isinstance(element, list) and list_item not in result:
        result.append(list_item)

    elif isinstance(element, list)and element not in result:
        result.append(element)

这对我有用:

 pool_list = []
    #Remove Nested Pools
    def reemovNestings(l):
        for i in l: 
            #if type(i) == list: 
            if(True==any(isinstance(el, list) for el in i)):
                reemovNestings(i) 
            else: 
            pool_list.append(i)

    reemovNestings(pool)
    print("Nested Pools Removed : \n" ,pool_list , "\n")

    #Remove identical pools
    unique = []
    for i in pool_list:
        if i not in unique:
            unique.append(i)

    print("Final Pool : \n ", unique)

如果您有一个平面列表并且不需要保留顺序

def unique(input):
  return list(set(input))

如果保留顺序很重要,您需要一个有序的字典。

Python 有有序集吗?

暂无
暂无

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

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