1

Hey i need some help regarding list of list. For example i have a list :

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

Here "parentList" is one list containing multiple sublists

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

and

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

I want to merge these two lists of lists within parentList if there is some common list between them. As you can clearly see in the example, that there is one list common in between A and B. I want the output as ```

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

Note:- there can be multiple sublists in "parentList" with nothing is common to any sublist. Such unique sublists should retain their structure.

Ma0
  • 15,057
  • 4
  • 35
  • 65
AzkaGilani
  • 73
  • 1
  • 9

4 Answers4

0

Simple iterative worst case O(n3) solution would be to iterative over the lists and compare and merge if the match is found.

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

Prints this:

[['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', '']]
Aleksei Maide
  • 1,845
  • 1
  • 21
  • 23
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)

Not most efficient one, since you are searching, but I think it solves your problem. :)

arundeepak
  • 564
  • 2
  • 12
0

Just have idea with this

Try this:-

if any((x in A) for x in B):
    parentList.append([A,B])
print(parentList)
Narendra
  • 1,511
  • 1
  • 10
  • 20
0

Here is another rather complicated approach that may suits your needs:

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

Output:

['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', '']
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29