给出了一个表格数据 struct ,我已经将其命名为pop(0)
,但为了清楚起见,将显示标题.基本上它是一个字符串格式的id
和两个浮点格式的值条目.
#header
['id','value1','value2']
data=
['1001',25,10],
['1001',25,0],
['1002',100,0],
['1002',100,0],
['1002',100,0],
['1003',150,10]...
并给出一个只包含唯一ids
的向量:
ids = ['1001','1002','1003']
我的目标是创建另一个存储所有value1
之和的向量(现在可以忽略value2
),同时保持ids
中的顺序.所需输出:
print(ids, sum_result)
1001 50
1002 300
1003 150
我在DICT方法上取得了一些进展,但后来我痛苦地意识到,秩序并没有得到维护.
问题
How might we iterate over each unique element in `ids` and sum all occurrences of that id in `data`? In my actual data, there are also some "Nones" and "NaN"s for `value1s` if that helps narrow the solution set for anyone.Note:%的人更喜欢原生Python