消除重复并对列表中的相应项求和
给定这两个列表,
L2 = [A,B,C,D,A,B]
L3 = [3,2,1,2,2,1]
我想获得
L2_WANTED = [A,B,C,D]
L3_WANTED = [5,3,1,2]
这些列表总是有序的,并且相同的大小和元素对应于键值对,例如A:3,B:2等。
目标是消除 L2 中的重复项并对 L3 中的相应项求和以获得具有匹配对的新列表。这是为了在将项目添加到列表时保留项目的运行列表。
我尝试用 index
编写一个函数,但它开始变得丑陋。我检查了 itertools,但找不到任何相关内容;我查看了 starmap()
但无法使其工作。也许这也可以通过列表理解来完成。我将不胜感激有关如何实现这种最简单方法的任何线索或指示。谢谢。
编辑
@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}
>>>
这如何消除欺骗并添加数字?你能提供线索吗?谢谢。
Given these 2 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可以做到,但根据 pyfunc,有更好的方法:
This will do it, but as per pyfunc, there are better ways:
我认为使用
zip
是组合列表的好方法。 dict.update 部分将进行求和,因为我获取了先前的值并更新它:输出:
{'A': 5, 'C': 1, 'B': 3, 'D': 2}
编辑:
虽然上面的方法很好,但我也考虑使用 itertools.izip 允许您按照自己的方式进行
zip
建立字典。这样你就可以节省内存。您需要做的就是在导入iterools
后将zip
替换为itertools.izip
I think using
zip
is a nice way to combine the lists. Thedict.update
portion will do the summing since I fetch the previous value and update it: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 replacezip
withitertools.izip
after importingiterools
我确信那里有更优雅的答案,并且会在回复中出现。
但对于一些简单的答案:
输出:
[为理解第二种实现而编辑的答案]
因此,在 for i in range(0, len(L2)): ... i 成为索引
使用此索引,您可以从 L3 中提取信息并L2 通过执行以下操作:
然后将信息添加到字典中,
如果键不存在,则此处 l2_sum.get(key, 0) 返回 0,否则返回当前值。
我希望它足够清楚。
I am sure there are more elegant answer there and would come in the replies.
But for some simple answers:
Output:
[Edited answer for understanding the second implementation]
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:
Then you add information to the dict
Here l2_sum.get(key, 0) returns 0 if the key is not present otherwise the current value.
I hope it is clear enough.