【问题标题】:Get unique objects from multiple lists and lists of lists. Then create a new list with unique objects from all the lists从多个列表和列表列表中获取唯一对象。然后使用所有列表中的唯一对象创建一个新列表
【发布时间】:2020-01-19 03:57:05
【问题描述】:

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

[[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)

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

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

Unique lists from a list

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

Creating unique list of objects from multiple lists

【问题讨论】:

    标签: python


    【解决方案1】:

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

    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]]
    

    【讨论】:

      【解决方案2】:

      假设您的列表设置为名为 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]

      【讨论】:

        【解决方案3】:

        您需要迭代数据集中的每个元素以检查它是否为 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)
        

        【讨论】:

          【解决方案4】:

          这对我有用:

           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)
          

          【讨论】:

            【解决方案5】:

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

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

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

            Does Python have an ordered set?

            【讨论】:

              猜你喜欢
              • 2011-10-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-02-11
              • 1970-01-01
              • 2021-05-02
              • 1970-01-01
              相关资源
              最近更新 更多