消除重复并对列表中的相应项求和

发布于 2024-09-30 13:12:12 字数 767 浏览 6 评论 0原文

给定这两个列表,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枕头说它不想醒 2024-10-07 13:12:19

这可以做到,但根据 pyfunc,有更好的方法:

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]

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]
澜川若宁 2024-10-07 13:12:18

我认为使用 zip 是组合列表的好方法。 dict.update 部分将进行求和,因为我获取了先前的值并更新它:

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

输出:
{'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. 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

玩心态 2024-10-07 13:12:17

我确信那里有更优雅的答案,并且会在回复中出现。

但对于一些简单的答案:

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

输出:

['A', 'B', 'C', 'D']
[5, 3, 1, 2]

[为理解第二种实现而编辑的答案]

>>> L3 = [3,2,1,2,2,1]
>>> L2 = ['A','B','C','D','A','B']
>>> range(0, len(L2))
[0, 1, 2, 3, 4, 5]
>>> 

因此,在 for i in range(0, len(L2)): ... i 成为索引

使用此索引,您可以从 L3 中提取信息并L2 通过执行以下操作:

key = L2[i]
num = L3[i]

然后将信息添加到字典中,

l2_sum[key] = l2_sum.get(key, 0) + num

如果键不存在,则此处 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文