473,507 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

merging of lists

32 New Member
Expand|Select|Wrap|Line Numbers
  1. aList = [1,2,3,4,5]
  2. bList = [4,5,6,7,8]
  3. cList = [7,8,9,10,11]
  4.  
  5. dList = [100,200,300]
  6. eList = [100,400,500]
  7.  
  8. list1 = list(set(aList) | set(bList) |set(cList))
  9. list2 = list(set(dList) | set(eList))
  10.  
  11. print list1,list2
  12.  
I have "n" lists each having a set of integer values. I would like to merge the list and get resulant or resultants list if there is at least one element that is common in both the list.

In the above sample code, I see aList and bList have common elements and similarly bList and cList. Hence I obtained list1 merging this. Similary dList and eList are having common element and hence merged dList and eList as list2.

I would like to know how to write a consice code looping over each list to obtain the resultant(s) list.

Thanks in advance.

SKN
Aug 16 '07 #1
14 2358
ghostdog74
511 Recognized Expert Contributor
Expand|Select|Wrap|Line Numbers
  1. aList = [1,2,3,4,5]
  2. bList = [4,5,6,7,8]
  3. cList = [7,8,9,10,11]
  4.  
  5. dList = [100,200,300]
  6. eList = [100,400,500]
  7.  
  8. list1 = list(set(aList) | set(bList) |set(cList))
  9. list2 = list(set(dList) | set(eList))
  10.  
  11. print list1,list2
  12.  
I have "n" lists each having a set of integer values. I would like to merge the list and get resulant or resultants list if there is at least one element that is common in both the list.

In the above sample code, I see aList and bList have common elements and similarly bList and cList. Hence I obtained list1 merging this. Similary dList and eList are having common element and hence merged dList and eList as list2.

I would like to know how to write a consice code looping over each list to obtain the resultant(s) list.

Thanks in advance.

SKN
Expand|Select|Wrap|Line Numbers
  1. >>> set(aList+bList+cList)
  2. set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
  3.  
Aug 16 '07 #2
bartonc
6,596 Recognized Expert Expert
Yes. 5.7.1 Set Objects come in very handy. Thanks, GD.
Aug 16 '07 #3
hidrkannan
32 New Member
The problem is to identify the lists that have common elements programatically. I have shown as an example that shows aList, bList, & cList have common elements and hence merged as list1.

Actually, I need to loop over and compare 2 lists and if have common elements merge as one list, and continue through the lists again. I am looking for a concise code to get this.

Thanks
SKN
Aug 16 '07 #4
bartonc
6,596 Recognized Expert Expert
The problem is to identify the lists that have common elements programatically. I have shown as an example that shows aList, bList, & cList have common elements and hence merged as list1.

Actually, I need to loop over and compare 2 lists and if have common elements merge as one list, and continue through the lists again. I am looking for a concise code to get this.

Thanks
SKN
This will compare the 0th to the 1st, the 1st to the 2nd and the 2nd to the 0th, in that order:
Expand|Select|Wrap|Line Numbers
  1. >>> aList = [1,2,3,4,5]
  2. >>> bList = [4,5,6,7,8]
  3. >>> cList = [7,8,9,10,11]
  4. >>> allLists = [aList, bList, cList]
  5. >>> stopat = len(allLists) - 1
  6. >>> for i, thisList in enumerate(allLists):
  7. ...     nextIndex = (i + 1, 0)[i == stopat]
  8. ...     result = set(thisList).intersection(set(allLists[nextIndex]))
  9. ...     if result:
  10. ...         print result
  11. ...         
  12. set([4, 5])
  13. set([8, 7])
  14. >>> 
Aug 16 '07 #5
hidrkannan
32 New Member
This will compare the 0th to the 1st, the 1st to the 2nd and the 2nd to the 0th, in that order:
Expand|Select|Wrap|Line Numbers
  1. >>> aList = [1,2,3,4,5]
  2. >>> bList = [4,5,6,7,8]
  3. >>> cList = [7,8,9,10,11]
  4. >>> allLists = [aList, bList, cList]
  5. >>> stopat = len(allLists) - 1
  6. >>> for i, thisList in enumerate(allLists):
  7. ...     nextIndex = (i + 1, 0)[i == stopat]
  8. ...     result = set(thisList).intersection(set(allLists[nextIndex]))
  9. ...     if result:
  10. ...         print result
  11. ...         
  12. set([4, 5])
  13. set([8, 7])
  14. >>> 

I can substitute union in place of intersection to get the result. But is not still sufficient to get the desired result completely of getting two list as shown in my original posting.

Need your help.

Thanks
SKN
Aug 16 '07 #6
bartonc
6,596 Recognized Expert Expert
I can substitute union in place of intersection to get the result. But is not still sufficient to get the desired result completely of getting two list as shown in my original posting.

Need your help.

Thanks
SKN
Yep. You are right about that. I missed part of your spec and going sequentially doesn't quite to a complete job. Please post some of your work so that we may see where you have gotten on improving on my post. Thanks.
Aug 16 '07 #7
hidrkannan
32 New Member
Expand|Select|Wrap|Line Numbers
  1. aList = [1,2,3,4,5]
  2. bList = [4,5,6,7,8]
  3. cList = [7,8,9,10,11]
  4. dList = [100,200,300,400]
  5. eList = [100,600,700]
  6. allLists = [aList, bList, cList,dList,eList]
  7. flag = 1
  8. resultList = []
  9. while (flag == 1):
  10.     listCount = len(allLists)
  11.     count = 0
  12.     removeList = []
  13.     for i in range(0,listCount-1):        
  14.         if len((set(allLists[0]) & set(allLists[i+1]))) > 0:
  15.             count = count + 1
  16.             set(allLists[0]).union(set(allLists[i+1]))
  17.             removeList.append(allLists[i+1])
  18.             count = count + 1
  19.             allLists[0] = list(set(allLists[0]).union(set(allLists[i+1])))            
  20.     if len(allLists):
  21.         resultList.append(allLists[0])
  22.         allLists.remove(allLists[0])
  23.     for tempList in removeList:
  24.         if tempList in allLists:
  25.             allLists.remove(tempList)
  26.     if count == 0:
  27.         flag = 0
  28. print resultList
  29.  

The above code does the job, but I feel it is lengthy and rounabout...

Thanks
SKN
Aug 17 '07 #8
bartonc
6,596 Recognized Expert Expert
Expand|Select|Wrap|Line Numbers
  1. aList = [1,2,3,4,5]
  2. bList = [4,5,6,7,8]
  3. cList = [7,8,9,10,11]
  4. dList = [100,200,300,400]
  5. eList = [100,600,700]
  6. allLists = [aList, bList, cList,dList,eList]
  7. flag = 1
  8. resultList = []
  9. while (flag == 1):
  10.     listCount = len(allLists)
  11.     count = 0
  12.     removeList = []
  13.     for i in range(0,listCount-1):        
  14.         if len((set(allLists[0]) & set(allLists[i+1]))) > 0:
  15.             count = count + 1
  16.             set(allLists[0]).union(set(allLists[i+1]))
  17.             removeList.append(allLists[i+1])
  18.             count = count + 1
  19.             allLists[0] = list(set(allLists[0]).union(set(allLists[i+1])))            
  20.     if len(allLists):
  21.         resultList.append(allLists[0])
  22.         allLists.remove(allLists[0])
  23.     for tempList in removeList:
  24.         if tempList in allLists:
  25.             allLists.remove(tempList)
  26.     if count == 0:
  27.         flag = 0
  28. print resultList
  29.  

The above code does the job, but I feel it is lengthy and rounabout...

Thanks
SKN
Here it is a bit simplified. But, if you'll notice, with the order of allLists rearranged, the algorithm is broken. I'm working on a fix, though.
Expand|Select|Wrap|Line Numbers
  1. aList = [1, 2, 3, 4, 5]
  2. bList = [4, 5, 6, 7, 8]
  3. cList = [7, 8, 9, 10, 11]
  4. dList = [100, 200, 300, 400]
  5. eList = [100, 600, 700]
  6. allLists = [dList, cList, aList, bList, eList]
  7.  
  8.  
  9. resultList = []
  10. while allLists:
  11.     listCount = len(allLists)
  12.     removeList = []
  13.     for i in range(listCount - 1):
  14.         if (set(allLists[0]) & set(allLists[i+1])):
  15.             removeList.append(allLists[i+1])
  16.  
  17.             allLists[0] = list(set(allLists[0]).union(set(allLists[i+1])))
  18.  
  19.     if allLists:
  20.         tempList = allLists.pop(0)
  21.         resultList.append(tempList)
  22.  
  23.     for tempList in removeList:
  24.         if tempList in allLists:
  25.             allLists.remove(tempList)
  26.  
  27. print resultList
[[400, 600, 100, 200, 700, 300], [4, 5, 6, 7, 8, 9, 10, 11], [1, 2, 3, 4, 5]]
Aug 17 '07 #9
bartonc
6,596 Recognized Expert Expert
Here it is a bit simplified. But, if you'll notice, with the order of allLists rearranged, the algorithm is broken. I'm working on a fix, though.
Expand|Select|Wrap|Line Numbers
  1. aList = [1, 2, 3, 4, 5]
  2. bList = [4, 5, 6, 7, 8]
  3. cList = [7, 8, 9, 10, 11]
  4. dList = [100, 200, 300, 400]
  5. eList = [100, 600, 700]
  6. allLists = [dList, cList, aList, bList, eList]
  7.  
  8.  
  9. resultList = []
  10. while allLists:
  11.     listCount = len(allLists)
  12.     removeList = []
  13.     for i in range(listCount - 1):
  14.         if (set(allLists[0]) & set(allLists[i+1])):
  15.             removeList.append(allLists[i+1])
  16.  
  17.             allLists[0] = list(set(allLists[0]).union(set(allLists[i+1])))
  18.  
  19.     if allLists:
  20.         tempList = allLists.pop(0)
  21.         resultList.append(tempList)
  22.  
  23.     for tempList in removeList:
  24.         if tempList in allLists:
  25.             allLists.remove(tempList)
  26.  
  27. print resultList
[[400, 600, 100, 200, 700, 300], [4, 5, 6, 7, 8, 9, 10, 11], [1, 2, 3, 4, 5]]
Just to show you my thinking: Here is a function that is just a bit simpler than the last and still works the same. Eventually this will become a recursive function...
Expand|Select|Wrap|Line Numbers
  1. def ListUnionFinder(someLists, aList = []):
  2.     resultList = []
  3.     while someLists:
  4.         tempList = someLists.pop(0)
  5.         listCount = len(someLists)
  6.         removeList = []
  7.         for i in range(listCount - 1):
  8.             if (set(tempList) & set(someLists[i+1])):
  9.                 removeList.append(someLists[i+1])
  10.  
  11.                 tempList = list(set(tempList).union(set(someLists[i+1])))
  12.  
  13.         resultList.append(tempList)
  14.  
  15.         for tempList in removeList:
  16.             if tempList in someLists:
  17.                 someLists.remove(tempList)
  18.  
  19.     print resultList
  20.  
  21. ListUnionFinder(allLists)
Aug 17 '07 #10
hidrkannan
32 New Member
Expand|Select|Wrap|Line Numbers
  1.  
  2. eList = [1,2,3,4,5]
  3. cList = [4,5,6,7,8]
  4. bList = [7,8,9,10,11]
  5. aList = [100,200,300,400]
  6. dList = [101,600,700]
  7. allLists = [aList, bList, cList,dList,eList]
  8. flag = 1
  9. resultList = []
  10. numList = len(allLists)
  11. numListCount = 0
  12. while (flag == 1):
  13.     numListCount = numListCount + 1
  14.     listCount = len(allLists)
  15.     count = 0
  16.     removeList = []
  17.     for i in range(0,listCount-1):    
  18.         if len((set(allLists[0]) & set(allLists[i+1]))) > 0:
  19.             count = count + 1
  20.             set(allLists[0]).union(set(allLists[i+1]))
  21.             removeList.append(allLists[i+1])
  22.             count = count + 1
  23.             allLists[0] = list(set(allLists[0]).union(set(allLists[i+1])))      
  24.     if len(allLists):
  25.         resultList.append(allLists[0])
  26.         allLists.remove(allLists[0])
  27.     for tempList in removeList:
  28.         if tempList in allLists:
  29.             allLists.remove(tempList)
  30.     if (numListCount == numList) and (count == 0):
  31.         flag = 0
  32. print resultList
  33.  
After testing further, I have corrected the previous posted code.

Thanks
SKN
Aug 17 '07 #11
bvdet
2,851 Recognized Expert Moderator Specialist
Expand|Select|Wrap|Line Numbers
  1. aList = [1,2,3,4,5]
  2. bList = [4,5,6,7,8]
  3. cList = [7,8,9,10,11]
  4. dList = [100,200,300,400]
  5. eList = [100,600,700]
  6. allLists = [aList, bList, cList,dList,eList]
  7. flag = 1
  8. resultList = []
  9. while (flag == 1):
  10.     listCount = len(allLists)
  11.     count = 0
  12.     removeList = []
  13.     for i in range(0,listCount-1):        
  14.         if len((set(allLists[0]) & set(allLists[i+1]))) > 0:
  15.             count = count + 1
  16.             set(allLists[0]).union(set(allLists[i+1]))
  17.             removeList.append(allLists[i+1])
  18.             count = count + 1
  19.             allLists[0] = list(set(allLists[0]).union(set(allLists[i+1])))            
  20.     if len(allLists):
  21.         resultList.append(allLists[0])
  22.         allLists.remove(allLists[0])
  23.     for tempList in removeList:
  24.         if tempList in allLists:
  25.             allLists.remove(tempList)
  26.     if count == 0:
  27.         flag = 0
  28. print resultList
  29.  

The above code does the job, but I feel it is lengthy and rounabout...

Thanks
SKN
I am not sure this is what you are after:
Expand|Select|Wrap|Line Numbers
  1. def merge_lists(*args):
  2.     '''
  3.     args is a list of lists in the format:
  4.     [[[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11]], \
  5.      [[100,200,300,400], [100,600,700]]
  6.     ]
  7.     return list of lists in the format:
  8.      [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [400, 600, 100, 200, 700, 300]]
  9.     '''
  10.     resultList = []
  11.     for arg in args:
  12.         tem = []
  13.         for item in arg:
  14.             tem += item
  15.         resultList.append(list(set(tem)))
  16.     return resultList
  17.  
  18. a = [[[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11]], [[100,200,300,400], [100,600,700]]]
  19.  
  20. print merge_lists(*a)
  21. print merge_lists([[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11]], [[100,200,300,400], [100,600,700]])
Output:
>>> [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [100, 200, 300, 400, 600, 700]]
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [100, 200, 300, 400, 600, 700]]
>>>
Aug 17 '07 #12
hidrkannan
32 New Member
Not really BV. Each of the five list is individually defined. Not grouped as list of 3 lists and list of 2 list, as in your input.

It may not be in that order also, as bartonc mentioned. The last code I posted works for:
1. Even if all the 5 lists contains elements that are execlusive to each other.
2. The 5 lists can be defined in any order (not necessarily in the order it needs to be grouped).
3. The general input is "n" list.

I think, the code I posted is "crude" not exploiting the true python capabilities.

Looking forward for your next version.

Thanks
SKN
Aug 17 '07 #13
bvdet
2,851 Recognized Expert Moderator Specialist
I am not sure this is what you are after:
Expand|Select|Wrap|Line Numbers
  1. def merge_lists(*args):
  2.     '''
  3.     args is a list of lists in the format:
  4.     [[[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11]], \
  5.      [[100,200,300,400], [100,600,700]]
  6.     ]
  7.     return list of lists in the format:
  8.      [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [400, 600, 100, 200, 700, 300]]
  9.     '''
  10.     resultList = []
  11.     for arg in args:
  12.         tem = []
  13.         for item in arg:
  14.             tem += item
  15.         resultList.append(list(set(tem)))
  16.     return resultList
  17.  
  18. a = [[[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11]], [[100,200,300,400], [100,600,700]]]
  19.  
  20. print merge_lists(*a)
  21. print merge_lists([[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11]], [[100,200,300,400], [100,600,700]])
Output:
>>> [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [100, 200, 300, 400, 600, 700]]
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [100, 200, 300, 400, 600, 700]]
>>>
I did not understand your requirement to merge list items having common elements. No wonder it was so easy!
I believe this code will do that, but if a list has an element or elements common in two different lists that have no common elements, it will merge with the first one and all will be merged:
Expand|Select|Wrap|Line Numbers
  1. def merge_lists(*args):
  2.     '''
  3.     args is a tuple of lists in the format:
  4.      [1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11], [100, 200, 300, 400], [100, 600, 700]
  5.     Merge the lists that have common elements
  6.     Return list of lists in the format:
  7.      [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [400, 600, 100, 200, 700, 300]]
  8.     '''
  9.     args = list(args)
  10.     resultList = [set(args.pop(0))]
  11.     while args:
  12.         removeList = []
  13.         for arg in args[:]:
  14.             for i, item in enumerate(resultList):
  15.                 if set(arg).intersection(item):
  16.                     resultList[i] |= set(arg)
  17.                     removeList.append(arg)
  18.                     break
  19.         for item in removeList:
  20.             args.remove(item)
  21.         if args:
  22.             resultList.append(set(args.pop(0)))
  23.     return resultList
Interaction:
Expand|Select|Wrap|Line Numbers
  1. >>> merge_lists([1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11], [100, 200, 300, 400], [100, 600, 700])
  2. [Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]), Set([400, 600, 100, 200, 700, 300])]
  3. >>> merge_lists([1,100], [1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11], [100, 200, 300, 400], [100, 600, 700])
  4. [Set([1, 2, 3, 100, 5, 6, 7, 8, 9, 10, 11, 300, 400, 200, 600, 4, 700])]
  5. >>> 
Aug 17 '07 #14
hidrkannan
32 New Member
This is what I was looking for. Thanks BV & bartonc

SKN
Aug 17 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

24
3913
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
3
1870
by: Patrick | last post by:
I have got 2 XML documents, both of which conform to the same XSD Schema, which define possible optional elements. The 2 XML documents contain 2 disjoint set of XML elements. What is the best,...
1
4913
by: svdh | last post by:
I have posed a question last saturday and have advanced alot in the meantime. But I am still not there Problem is that I try to merging various fields from various tables in one document in Word...
2
1702
by: teresaalmond | last post by:
I routinely have to qry several databases and combine the records for a mailing list. In all of the tables the fiedl names are different. For example: FName First name and FirstName Right not I...
15
1446
by: PRadyut | last post by:
In this code it throws a runtime error on a code access violation . On the line z->data=p->data; while (p!=NULL && q!=NULL) { if (*s==NULL) {
3
6537
by: ptrSriram | last post by:
Can someone help me with an algorithm to merge two binary search trees. One method I thought of was to flatten both the trees into sorted lists(inorder traversal),merge those two sorted lists,...
9
4562
by: SMB | last post by:
I have two lists of data like the following: LIST1 , ] LIST2 , 'label': 'First Name', 'width': 0L, 'separator': ',', 'height': 0L, 'type': 2L, 'order': 1L}, {'code': 14L, 'name': 'Last...
14
3991
by: etal | last post by:
Here's an algorithm question: How should I efficiently merge a collection of mostly similar lists, with different lengths and arbitrary contents, while eliminating duplicates and preserving order...
4
2409
by: v13tn1g | last post by:
so basically i've written most of the code the only problem that i'm having is that the output that it gives me will not return a key with a different type. It will either return me keys with only...
0
7221
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7029
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.