根据另一个列表中的唯一值对列表中的值进行求和。

huangapple go评论84阅读模式
英文:

Sum values in a list based on the unique values in an other list

问题

我有两个列表,其中一个列表包含数值,另一个包含字符串("A","B"或"C")。目标是根据第二个列表中的唯一字符串来对值进行求和。我假设第一个列表是有序的(任意顺序),并且第二个列表中的索引与之相匹配。

示例:

list_one = ["A", "A", "B", "B", "C", "C"]
list_two = [1000, 200, 500, 120, 500, 350]

结果列表应该根据list_two中的值,对list_one中每个唯一的字符串进行求和:

res_list = [1200, 620, 850]

我可以通过以下方式找到list_one中每个唯一字符串的索引:

np.unique(list_one, return_index=True)[1] = array([0, 2, 4], dtype=int64)

但我不知道如何继续下一步。

英文:

I have two lists, where one lists contains numeric values and the other contains strings ("A", "B" or "C"). The goal is to sum up the values for every unique string in the second list. I assume the first list is ordered (arbitrary) and the indexes in second list match up.

Example:

list_one = ["A", "A", "B", "B", "C", "C"]
list_two = [1000, 200, 500, 120, 500, 350]

The resulting list should be the sum for each unique string in list_one based on the values in list_two:

res_list = [1200, 620, 850]

I can find the indexes of per unique string in list_one by

> np.unique(list_one, return_index=True)[1] = array([0, 2, 4], dtype=int64)

but I don't know how to go from here.

答案1

得分: 1

假设这两个列表的长度相同你可以使用一个字典作为中间手段来累积相关数据

    list_one = ["A", "A", "B", "B", "C", "C"]
    list_two = [1000, 200, 500, 120, 500, 350]
    
    r = {}
    
    for a, b in zip(list_one, list_two):
        r[a] = r.get(a, 0) + b
    
    print(list(r.values()))

**输出结果:**

    [1200, 620, 850]

你也可以使用`defaultdict`来写更为简洁的代码

    from collections import defaultdict
    
    list_one = ["A", "A", "B", "B", "C", "C"]
    list_two = [1000, 200, 500, 120, 500, 350]
    
    r = defaultdict(int)
    
    for a, b in zip(list_one, list_two):
        r[a] += b
    
    print(list(r.values()))
英文:

Assuming that both lists are of the same length you could use a dictionary as an intermediate means of accumulating the relevant data

list_one = ["A", "A", "B", "B", "C", "C"]
list_two = [1000, 200, 500, 120, 500, 350]

r = {}

for a, b in zip(list_one, list_two):
    r[a] = r.get(a, 0) + b

print(list(r.values()))

Output:

[1200, 620, 850]

You could also use a defaultdict for slightly more concise code as follows:

from collections import defaultdict

list_one = ["A", "A", "B", "B", "C", "C"]
list_two = [1000, 200, 500, 120, 500, 350]

r = defaultdict(int)

for a, b in zip(list_one, list_two):
    r[a] += b

print(list(r.values()))

答案2

得分: 0

你可以使用itertools中的groupby与列表之一上的迭代器。

像这样:

from itertools import groupby

res_list = [sum(next(i) for _ in g) for   i in [iter(list_two)] 
                                    for _,g in groupby(list_one)]

[1200, 620, 850]

或者像这样:

res_list = [sum(g) for i   in [iter(list_one)]
                   for _,g in groupby(list_two,key=lambda _:next(i))]
英文:

You could use groupby from itertools with an iterator on one of the lists.

Like this:

from itertools import groupby

res_list = [sum(next(i) for _ in g) for   i in [iter(list_two)] 
                                    for _,g in groupby(list_one)]

[1200, 620, 850]

or like this:

res_list = [sum(g) for i   in [iter(list_one)]
                   for _,g in groupby(list_two,key=lambda _:next(i))]

huangapple
  • 本文由 发表于 2023年7月3日 19:28:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定