-
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 2358 -
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
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: -
>>> 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
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.
-
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
bartonc 6,596
Recognized Expert Expert -
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]]
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. - 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
Recognized Expert Moderator Specialist -
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
Recognized Expert Moderator Specialist
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 = ...
|
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,...
|
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...
|
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...
|
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)
{
| |
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,...
|
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...
|
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...
|
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...
|
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,...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |