473,373 Members | 1,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,373 software developers and data experts.

merging of lists

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 2354
ghostdog74
511 Expert 256MB
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 Expert 4TB
Yes. 5.7.1 Set Objects come in very handy. Thanks, GD.
Aug 16 '07 #3
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 Expert 4TB
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
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 Expert 4TB
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
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 Expert 4TB
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 Expert 4TB
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
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 Expert Mod 2GB
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
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 Expert Mod 2GB
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
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
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
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
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
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
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
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
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
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
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.