Python-如何组合列表列表Iff在这些列表中有一些共同之处?

时间:2018-02-21 08:56:07

标签: python

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

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"中可以有多个子列表任何子列表都没有任何共同点。这些独特的子列表应保留其结构。

4 个答案:

答案 0 :(得分:0)

简单迭代最坏情况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', '']]

答案 1 :(得分:0)

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)

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

答案 2 :(得分:0)

只是对此有想法

试试这个: -

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

答案 3 :(得分:0)

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

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