【问题标题】:Python-How to combine list of lists Iff there is something common in those lists?Python-如何组合列表列表如果这些列表中有一些共同点?
【发布时间】:2018-02-21 08:56:06
【问题描述】:

嘿,我需要一些关于列表列表的帮助。例如我有一个列表:

parentList = [[["69", "742", "180","760", "05.07.2007", ""],
               ["69"," 768", "180", "785", "05.07.2007", ""], 
               ["69", "794","180","811","05072007",""], 
               ["69", "768", "180","785", "05.07.2007", ""]],
              [["69", "742", "180", "760", "05.07.2007", ""], 
               ["68", "717", "180", "735", "05.07.2007", ""]]]

这里的“parentList”是一个包含多个子列表的列表

A=[["69", "742", "180","760", "05.07.2007", ""],
   ["69"," 768", "180", "785", "05.07.2007", ""], 
   ["69", "794","180","811","05072007",""], 
   ["69", "768", "180","785", "05.07.2007", ""]]

B= [["69", "742", "180", "760", "05.07.2007", ""], 
    ["68", "717", "180", "735", "05.07.2007", ""]]

如果它们之间有一些共同的列表,我想将这两个列表合并到 parentList 中。正如您在示例中清楚地看到的那样,A 和 B 之间有一个共同的列表。 我希望输出为 ```

parentList = [[["69", "742", "180","760", "05.07.2007", ""],
               ["69"," 768", "180", "785", "05.07.2007", ""], 
               ["69", "794","180","811","05072007",""], 
               ["69", "768", "180","785", "05.07.2007", ""],
               ["69", "742", "180", "760", "05.07.2007", ""], 
               ["68", "717", "180", "735", "05.07.2007", ""]]]

注意:-“parentList”中可以有多个子列表,任何子列表都没有任何共同点。此类独特的子列表应保留其结构。

【问题讨论】:

  • 请修正格式,无法阅读问题
  • 最低级别的列表是否都一样长?
  • @AlekseiMaide 解决了这个问题。我希望它现在可以阅读
  • @morsecodist 不。长度可以是动态的

标签: python


【解决方案1】:

这是另一种相当复杂的方法,可能适合您的需求:

parentList = [[["69", "742", "180","760", "05.07.2007", ""],
               ["69"," 768", "180", "785", "05.07.2007", ""], 
               ["69", "794","180","811","05072007",""], 
               ["69", "768", "180","785", "05.07.2007", ""]],
              [["69", "742", "180", "760", "05.07.2007", ""], 
               ["68", "717", "180", "735", "05.07.2007", ""]]]

# Gather all elements of every sublist in a list along with the indice of the sublist e.g [0,1] of the parentList that they belong to.
elements = []
for subListIndex, subList in enumerate(parentList):
   for elem in subList:
      elements.append([subListIndex, elem])

# Check if two elements of different subList index have the same list and if they do, merge the two subLists.
for i in range(len(elements)):
   for j in [k for k in range(len(elements)) if not k==i]:
      if elements[i][1] == elements[j][1] and not elements[i][0]==elements[j][0]:
         parentList[elements[i][0]].extend(parentList[elements[j][0]])
         parentList[elements[j][0]] = [] # Needed in order to avoid reproducing already existing merged lists.

# Clear any empty subList.
parentList = [l for l in parentList if not l==[]]

# Print the final result.
for subList in parentList:
   for elem in subList:
      print(elem)
   print()

输出:

['69', '742', '180', '760', '05.07.2007', '']
['69', ' 768', '180', '785', '05.07.2007', '']
['69', '794', '180', '811', '05072007', '']
['69', '768', '180', '785', '05.07.2007', '']
['69', '742', '180', '760', '05.07.2007', '']
['68', '717', '180', '735', '05.07.2007', '']

【讨论】:

  • parentList[elements[j][0]].clear()没有方法clear()
  • 这绝对是我需要的。谢谢你:')
  • @Sanket 感谢您的指出。我编辑了我的问题。不过,使用这种方法确实有效。
【解决方案2】:
A=[["69", "742", "180","760", "05.07.2007", ""],
   ["69"," 768", "180", "785", "05.07.2007", ""], 
   ["69", "794","180","811","05072007",""], 
   ["69", "768", "180","785", "05.07.2007", ""]]

B= [["69", "742", "180", "760", "05.07.2007", ""], 
    ["68", "717", "180", "735", "05.07.2007", ""]]


found = False

for a in A:
    if a in B:
        found = True
        break

if found:
    print(A+B)

不是最有效的,因为您正在搜索,但我认为它可以解决您的问题。 :)

【讨论】:

    【解决方案3】:

    只是有这个想法

    试试这个:-

    if any((x in A) for x in B):
        parentList.append([A,B])
    print(parentList)
    

    【讨论】:

      【解决方案4】:

      简单的迭代最坏情况 O(n3) 解决方案是迭代列表并在找到匹配项时进行比较和合并。

      A=[["69", "742", "180","760", "05.07.2007", ""],
         ["69"," 768", "180", "785", "05.07.2007", ""], 
         ["69", "794","180","811","05072007",""], 
         ["69", "768", "180","785", "05.07.2007", ""]]
      
      B= [["69", "742", "180", "760", "05.07.2007", ""], 
          ["68", "717", "180", "735", "05.07.2007", ""]]
      
      def should_merge(A, B):
         if any((b in A) for b in B):
              return True
      
      def merge(A, B):
        result = A
        for b in B:
          if (b not in A):
            result.append(b)
        return result
      
      if should_merge(A, B):
        print(merge(A, B))
      

      打印这个:

      [['69', '742', '180', '760', '05.07.2007', ''], 
      ['69', ' 768', '180', '785', '05.07.2007', ''], 
      ['69', '794', '180', '811', '05072007', ''], 
      ['69', '768', '180', '785', '05.07.2007', ''], 
      ['68', '717', '180', '735', '05.07.2007', '']]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-11
        • 2021-11-23
        • 1970-01-01
        • 1970-01-01
        • 2019-02-01
        • 1970-01-01
        • 2015-09-19
        • 1970-01-01
        相关资源
        最近更新 更多