2 回答
TA贡献1802条经验 获得超4个赞
您可以zip将列表放在一起,用于groupby按f值对对进行分组,然后a对每个组的值求和。然后你只需要将它们解压缩回单独的列表
from itertools import groupby
from operator import itemgetter
groups = groupby(zip(f, a), key=itemgetter(0))
f_a_generator = ((k, sum(map(itemgetter(1), pairs))) for k, pairs in groups)
f1, a1 = zip(*f_a_generator) # map(list, ...) If you need them as lists
print(f1, a1, sep='\n')
# (10, 25, 50, 75, 100, 1000, 1100)
# (1, 3, 6, 5, 3, 25, 10)
要在评论中回答您的问题,您可以更改行
sum(map(itemgetter(1), pairs)))
调用某些函数,而不是sum:
def logarithmic_sum(values):
return 10*np.log10(sum((10**(val/10)) for val in values))
groups = groupby(zip(f, a), key=itemgetter(0))
f_a_generator = ((k, logarithmic_sum(map(itemgetter(1), pairs))) for k, pairs in groups)
f1, a1 = zip(*f_a_generator)
print(f1, a1, sep='\n')
# (10, 25, 50, 75, 100, 1000, 1100)
# (1.0000000000000002, 2.999999999999999, 6.124426027943397, 5.0, 2.999999999999999, 16.193310480660944, 8.010299956639813)
TA贡献1796条经验 获得超4个赞
对于第一个数组情况:
for i in range(1, len(f)-2):
if f[i] == f[i-1]:
del f[i]
print(f)
- 2 回答
- 0 关注
- 199 浏览
添加回答
举报