Given these 2开发者_JAVA技巧 lists
L2 = [A,B,C,D,A,B]
L3 = [3,2,1,2,2,1]
I want to obtain
L2_WANTED = [A,B,C,D]
L3_WANTED = [5,3,1,2]
The lists are always ordered and same size and elements correspond as key value pairs eg A:3, B:2 and so on.
The objective is to eliminate the dups in L2 and sum the corresponding terms in L3 to obtain a new list with matching pairs. This is to keep a running list of items as they are added to the lists.
I tried to write a function with index
but it started to get ugly. I checked itertools
but could not find anything that relates; I looked at starmap()
but couldn't make it work. Probably this can be done with list comprehension as well. I would appreciate any clues or directions about how to achieve this most simple way. Thank you.
EDİT
@SimonC:
>>> l2_sum = {}
>>> for i in range(0, len(L2)):
key = L2[i]
num = L3[i]
l2_sum[key] = l2_sum.get(key, 0) + num
>>> l2_sum
{'A': 5, 'C': 1, 'B': 3, 'D': 2}
>>>
How does this eliminate the dupes and add the numbers? Can you give a clue? Thanks.
I am sure there are more elegant answer there and would come in the replies.
But for some simple answers:
L2 = ['A','B','C','D','A','B']
L3 = [3,2,1,2,2,1]
L4 = zip(L2, L3)
L5 = []
L6 = []
def freduce(l):
for x, y in l:
print x , y
if x in L5:
k = L5.index(x)
L6[k] += y
else:
L5.append(x)
L6.append(y)
freduce(L4)
print L5
print L6
Output:
['A', 'B', 'C', 'D']
[5, 3, 1, 2]
[Edited answer for understanding the second implementation]
>>> L3 = [3,2,1,2,2,1]
>>> L2 = ['A','B','C','D','A','B']
>>> range(0, len(L2))
[0, 1, 2, 3, 4, 5]
>>>
Hence in for i in range(0, len(L2)): ... i becomes an index
Using this index, you could extract information from L3 and L2 by doing:
key = L2[i]
num = L3[i]
Then you add information to the dict
l2_sum[key] = l2_sum.get(key, 0) + num
Here l2_sum.get(key, 0) returns 0 if the key is not present otherwise the current value.
I hope it is clear enough.
I think using zip
is a nice way to combine the lists. The dict.update
portion will do the summing since I fetch the previous value and update it:
foo = dict()
for x, y in zip(['A', 'B', 'C', 'D', 'A', 'B'],
[3, 2, 1, 2, 2, 1]):
foo[x] = y + foo.get(x, 0)
print foo
Outputs:
{'A': 5, 'C': 1, 'B': 3, 'D': 2}
Edit:
While the above is fine, I'd also consider using itertools.izip which allows you to do the zip
as you build the dictionary. This way you'll save on memory. All you'd need to do is replace zip
with itertools.izip
after importing iterools
This will do it, but as per pyfunc, there are better ways:
l2_sum = {}
for i in range(0,len(L2)):
key = L2[i]
num = L3[i]
l2_sum[key] = l2_sum.get(key, 0) + num
L2_WANTED = sorted(l2_sum.keys())
L3_WANTED = [l2_sum[key] for key in L2_WANTED]
精彩评论