473,373 Members | 1,087 Online

# 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.

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.

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.

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.

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