python如果列表中有共同点,如何组合列表?

2024-04-27 04:37:02 发布

您现在位置: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", ""]]]

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

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

如果这两个列表之间有共同的列表,我想将它们合并到父列表中。正如您在示例中清楚地看到的,在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", ""]]]

注意:-在“父列表”中可以有多个子列表,但任何子列表都没有共同点。这种独特的子列表应该保留其结构。你知道吗


Tags: 示例列表结构名单共同点parentlist
3条回答

下面是另一个相当复杂的方法,可能适合您的需要:

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

你知道吗

试试这是:-

if any((x in A) for x in B):
    parentList.append([A,B])
print(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", ""]]


found = False

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

if found:
    print(A+B)

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

相关问题 更多 >