-
aList = [1,2,3,4,5]
-
bList = [4,5,6,7,8]
-
cList = [7,8,9,10,11]
-
-
dList = [100,200,300]
-
eList = [100,400,500]
-
-
list1 = list(set(aList) | set(bList) |set(cList))
-
list2 = list(set(dList) | set(eList))
-
-
print list1,list2
-
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
14 2182 -
aList = [1,2,3,4,5]
-
bList = [4,5,6,7,8]
-
cList = [7,8,9,10,11]
-
-
dList = [100,200,300]
-
eList = [100,400,500]
-
-
list1 = list(set(aList) | set(bList) |set(cList))
-
list2 = list(set(dList) | set(eList))
-
-
print list1,list2
-
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
-
>>> set(aList+bList+cList)
-
set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
-
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
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: -
>>> aList = [1,2,3,4,5]
-
>>> bList = [4,5,6,7,8]
-
>>> cList = [7,8,9,10,11]
-
>>> allLists = [aList, bList, cList]
-
>>> stopat = len(allLists) - 1
-
>>> for i, thisList in enumerate(allLists):
-
... nextIndex = (i + 1, 0)[i == stopat]
-
... result = set(thisList).intersection(set(allLists[nextIndex]))
-
... if result:
-
... print result
-
...
-
set([4, 5])
-
set([8, 7])
-
>>>
This will compare the 0th to the 1st, the 1st to the 2nd and the 2nd to the 0th, in that order: -
>>> aList = [1,2,3,4,5]
-
>>> bList = [4,5,6,7,8]
-
>>> cList = [7,8,9,10,11]
-
>>> allLists = [aList, bList, cList]
-
>>> stopat = len(allLists) - 1
-
>>> for i, thisList in enumerate(allLists):
-
... nextIndex = (i + 1, 0)[i == stopat]
-
... result = set(thisList).intersection(set(allLists[nextIndex]))
-
... if result:
-
... print result
-
...
-
set([4, 5])
-
set([8, 7])
-
>>>
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
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.
-
aList = [1,2,3,4,5]
-
bList = [4,5,6,7,8]
-
cList = [7,8,9,10,11]
-
dList = [100,200,300,400]
-
eList = [100,600,700]
-
allLists = [aList, bList, cList,dList,eList]
-
flag = 1
-
resultList = []
-
while (flag == 1):
-
listCount = len(allLists)
-
count = 0
-
removeList = []
-
for i in range(0,listCount-1):
-
if len((set(allLists[0]) & set(allLists[i+1]))) > 0:
-
count = count + 1
-
set(allLists[0]).union(set(allLists[i+1]))
-
removeList.append(allLists[i+1])
-
count = count + 1
-
allLists[0] = list(set(allLists[0]).union(set(allLists[i+1])))
-
if len(allLists):
-
resultList.append(allLists[0])
-
allLists.remove(allLists[0])
-
for tempList in removeList:
-
if tempList in allLists:
-
allLists.remove(tempList)
-
if count == 0:
-
flag = 0
-
print resultList
-
The above code does the job, but I feel it is lengthy and rounabout...
Thanks
SKN
-
aList = [1,2,3,4,5]
-
bList = [4,5,6,7,8]
-
cList = [7,8,9,10,11]
-
dList = [100,200,300,400]
-
eList = [100,600,700]
-
allLists = [aList, bList, cList,dList,eList]
-
flag = 1
-
resultList = []
-
while (flag == 1):
-
listCount = len(allLists)
-
count = 0
-
removeList = []
-
for i in range(0,listCount-1):
-
if len((set(allLists[0]) & set(allLists[i+1]))) > 0:
-
count = count + 1
-
set(allLists[0]).union(set(allLists[i+1]))
-
removeList.append(allLists[i+1])
-
count = count + 1
-
allLists[0] = list(set(allLists[0]).union(set(allLists[i+1])))
-
if len(allLists):
-
resultList.append(allLists[0])
-
allLists.remove(allLists[0])
-
for tempList in removeList:
-
if tempList in allLists:
-
allLists.remove(tempList)
-
if count == 0:
-
flag = 0
-
print resultList
-
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. - aList = [1, 2, 3, 4, 5]
-
bList = [4, 5, 6, 7, 8]
-
cList = [7, 8, 9, 10, 11]
-
dList = [100, 200, 300, 400]
-
eList = [100, 600, 700]
-
allLists = [dList, cList, aList, bList, eList]
-
-
-
resultList = []
-
while allLists:
-
listCount = len(allLists)
-
removeList = []
-
for i in range(listCount - 1):
-
if (set(allLists[0]) & set(allLists[i+1])):
-
removeList.append(allLists[i+1])
-
-
allLists[0] = list(set(allLists[0]).union(set(allLists[i+1])))
-
-
if allLists:
-
tempList = allLists.pop(0)
-
resultList.append(tempList)
-
-
for tempList in removeList:
-
if tempList in allLists:
-
allLists.remove(tempList)
-
-
print resultList
[[400, 600, 100, 200, 700, 300], [4, 5, 6, 7, 8, 9, 10, 11], [1, 2, 3, 4, 5]]
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. - aList = [1, 2, 3, 4, 5]
-
bList = [4, 5, 6, 7, 8]
-
cList = [7, 8, 9, 10, 11]
-
dList = [100, 200, 300, 400]
-
eList = [100, 600, 700]
-
allLists = [dList, cList, aList, bList, eList]
-
-
-
resultList = []
-
while allLists:
-
listCount = len(allLists)
-
removeList = []
-
for i in range(listCount - 1):
-
if (set(allLists[0]) & set(allLists[i+1])):
-
removeList.append(allLists[i+1])
-
-
allLists[0] = list(set(allLists[0]).union(set(allLists[i+1])))
-
-
if allLists:
-
tempList = allLists.pop(0)
-
resultList.append(tempList)
-
-
for tempList in removeList:
-
if tempList in allLists:
-
allLists.remove(tempList)
-
-
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... -
def ListUnionFinder(someLists, aList = []):
-
resultList = []
-
while someLists:
-
tempList = someLists.pop(0)
-
listCount = len(someLists)
-
removeList = []
-
for i in range(listCount - 1):
-
if (set(tempList) & set(someLists[i+1])):
-
removeList.append(someLists[i+1])
-
-
tempList = list(set(tempList).union(set(someLists[i+1])))
-
-
resultList.append(tempList)
-
-
for tempList in removeList:
-
if tempList in someLists:
-
someLists.remove(tempList)
-
-
print resultList
-
-
ListUnionFinder(allLists)
-
-
eList = [1,2,3,4,5]
-
cList = [4,5,6,7,8]
-
bList = [7,8,9,10,11]
-
aList = [100,200,300,400]
-
dList = [101,600,700]
-
allLists = [aList, bList, cList,dList,eList]
-
flag = 1
-
resultList = []
-
numList = len(allLists)
-
numListCount = 0
-
while (flag == 1):
-
numListCount = numListCount + 1
-
listCount = len(allLists)
-
count = 0
-
removeList = []
-
for i in range(0,listCount-1):
-
if len((set(allLists[0]) & set(allLists[i+1]))) > 0:
-
count = count + 1
-
set(allLists[0]).union(set(allLists[i+1]))
-
removeList.append(allLists[i+1])
-
count = count + 1
-
allLists[0] = list(set(allLists[0]).union(set(allLists[i+1])))
-
if len(allLists):
-
resultList.append(allLists[0])
-
allLists.remove(allLists[0])
-
for tempList in removeList:
-
if tempList in allLists:
-
allLists.remove(tempList)
-
if (numListCount == numList) and (count == 0):
-
flag = 0
-
print resultList
-
After testing further, I have corrected the previous posted code.
Thanks
SKN
bvdet 2,851
Expert Mod 2GB -
aList = [1,2,3,4,5]
-
bList = [4,5,6,7,8]
-
cList = [7,8,9,10,11]
-
dList = [100,200,300,400]
-
eList = [100,600,700]
-
allLists = [aList, bList, cList,dList,eList]
-
flag = 1
-
resultList = []
-
while (flag == 1):
-
listCount = len(allLists)
-
count = 0
-
removeList = []
-
for i in range(0,listCount-1):
-
if len((set(allLists[0]) & set(allLists[i+1]))) > 0:
-
count = count + 1
-
set(allLists[0]).union(set(allLists[i+1]))
-
removeList.append(allLists[i+1])
-
count = count + 1
-
allLists[0] = list(set(allLists[0]).union(set(allLists[i+1])))
-
if len(allLists):
-
resultList.append(allLists[0])
-
allLists.remove(allLists[0])
-
for tempList in removeList:
-
if tempList in allLists:
-
allLists.remove(tempList)
-
if count == 0:
-
flag = 0
-
print resultList
-
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: - def merge_lists(*args):
-
'''
-
args is a list of lists in the format:
-
[[[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11]], \
-
[[100,200,300,400], [100,600,700]]
-
]
-
return list of lists in the format:
-
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [400, 600, 100, 200, 700, 300]]
-
'''
-
resultList = []
-
for arg in args:
-
tem = []
-
for item in arg:
-
tem += item
-
resultList.append(list(set(tem)))
-
return resultList
-
-
a = [[[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11]], [[100,200,300,400], [100,600,700]]]
-
-
print merge_lists(*a)
-
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]]
>>>
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
bvdet 2,851
Expert Mod 2GB
I am not sure this is what you are after: - def merge_lists(*args):
-
'''
-
args is a list of lists in the format:
-
[[[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11]], \
-
[[100,200,300,400], [100,600,700]]
-
]
-
return list of lists in the format:
-
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [400, 600, 100, 200, 700, 300]]
-
'''
-
resultList = []
-
for arg in args:
-
tem = []
-
for item in arg:
-
tem += item
-
resultList.append(list(set(tem)))
-
return resultList
-
-
a = [[[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11]], [[100,200,300,400], [100,600,700]]]
-
-
print merge_lists(*a)
-
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: - def merge_lists(*args):
-
'''
-
args is a tuple of lists in the format:
-
[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11], [100, 200, 300, 400], [100, 600, 700]
-
Merge the lists that have common elements
-
Return list of lists in the format:
-
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [400, 600, 100, 200, 700, 300]]
-
'''
-
args = list(args)
-
resultList = [set(args.pop(0))]
-
while args:
-
removeList = []
-
for arg in args[:]:
-
for i, item in enumerate(resultList):
-
if set(arg).intersection(item):
-
resultList[i] |= set(arg)
-
removeList.append(arg)
-
break
-
for item in removeList:
-
args.remove(item)
-
if args:
-
resultList.append(set(args.pop(0)))
-
return resultList
Interaction: - >>> merge_lists([1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11], [100, 200, 300, 400], [100, 600, 700])
-
[Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]), Set([400, 600, 100, 200, 700, 300])]
-
>>> 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])
-
[Set([1, 2, 3, 100, 5, 6, 7, 8, 9, 10, 11, 300, 400, 200, 600, 4, 700])]
-
>>>
This is what I was looking for. Thanks BV & bartonc
SKN
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
24 posts
views
Thread by Lasse Vågsæther Karlsen |
last post: by
|
3 posts
views
Thread by Patrick |
last post: by
|
1 post
views
Thread by svdh |
last post: by
|
2 posts
views
Thread by teresaalmond |
last post: by
|
15 posts
views
Thread by PRadyut |
last post: by
|
3 posts
views
Thread by ptrSriram |
last post: by
|
9 posts
views
Thread by SMB |
last post: by
|
14 posts
views
Thread by etal |
last post: by
| | | | | | | | | | | |