在Python中的浮点数列表中对数据进行分组

我正在从API接收以下订单数据:

myData = [[9656.37, 0.063], [9656.32, 0.688], [9656.21, 0.729], [9656.07, 0.618], [9656.01, 0.598], [9656.0, 0.917], [9655.92, 0.417], [9655.84, 0.403], [9655.81, 1.032], [9655.6, 1.394], [9655.5, 0.043], [9655.41, 0.6], [9655.32, 0.441], [9655.31, 3.359], [9655.29, 0.15], [9655.0, 11.463], [9654.89, 0.612], [9654.88, 0.613], [9654.87, 0.602], [9654.74, 0.382], [9654.69, 0.75], [9654.63, 8.106], [9654.62, 0.8], [9654.58, 0.12], [9654.44, 0.54], [9654.38, 0.311], [9654.33, 2.758], [9654.31, 0.72], [9654.25, 0.02], [9654.14, 4.0], [9654.0, 0.155], [9653.95, 0.642], [9653.91, 0.5], [9653.83, 0.063], [9653.81, 0.6], [9653.38, 0.05], [9653.34, 0.1], [9653.31, 0.207], [9653.3, 0.001], [9653.28, 0.008], [9653.25, 3.351], [9653.03, 10.0], [9653.02, 0.651], [9652.98, 0.105], [9652.92, 0.002], [9652.9, 3.589], [9652.82, 2.07], [9652.8, 1.25], [9652.7, 0.4], [9652.66, 5.5], [9652.5, 0.006], [9652.48, 0.01], [9652.4, 0.207], [9652.39, 2.799], [9652.21, 0.174], [9652.14, 0.063], [9652.12, 8.106], [9652.09, 1.591], [9652.07, 4.0], [9652.06, 0.16], [9652.0, 0.006], [9651.9, 0.858], [9651.5, 0.006], [9651.49, 0.207], [9651.45, 0.106], [9651.37, 0.001], [9651.28, 0.7], [9651.21, 0.001], [9651.2, 1.377], [9651.16, 0.613], [9651.07, 0.16], [9651.06, 0.64], [9651.01, 0.75], [9651.0, 1.001], [9650.99, 2.637], [9650.94, 0.063], [9650.93, 2.82], [9650.87, 0.098], [9650.77, 4.782], [9650.6, 0.001], [9650.59, 12.279], [9650.56, 0.008], [9650.4, 0.008], [9650.23, 0.622], [9650.21, 0.008], [9650.2, 0.008], [9650.1, 8.106], [9650.09, 0.5], [9650.05, 1.036], [9650.0, 7.573], [9649.89, 0.5], [9649.76, 2.461], [9649.68, 0.207], [9649.6, 0.016], [9649.48, 0.059], [9649.4, 0.008], [9649.3, 10.0], [9649.29, 0.651], [9649.25, 1.286], [9649.21, 0.008] .... ]

这只是数据的一部分,总列表很大,大约有999个元素。由于此列表不必要地太大,因此我在考虑减少它的方法,这是我想出的:

Grouping the data: instead of having 9656.37, 9656.32, 9656.21 i can have only 9656. This would receive the amount of data a lot. The problem with this, is that i would also need to sum the second elements of every element, so in this case (see the data above), 9656 = 0.063 + 0.688 + 0.729 + 0.598 + 0.917. Then, the same should be done with 9655 and so on.

I don't have high knowledge of Python yet, and every time i try to solve this i end up having a lot of for loops that make the code a mess, without getting closer to my expected output: [[9656, 3.613], [9655, 19.302]...]

是否有任何有关执行此特定任务的示例,一些内置功能或实践可以帮助我实现这一目标?任何形式的建议,不胜感激,谢谢

评论
你已被淘汰
你已被淘汰

You can use itertools.groupby for this, grouping on the int value of the first element of each sublist, and then summing the second elements of each sublist over the group:

import itertools
newData = [[k, sum(v[1] for v in g)] for k, g in itertools.groupby(myData, lambda v: int(v[0]))]

输出:

[
 [9656, 3.6129999999999995],
 [9655, 19.302],
 [9654, 20.488999999999997],
 [9653, 16.173000000000002],
 [9652, 30.038],
 [9651, 6.42],
 [9650, 40.549],
 [9649, 15.195999999999998]
]
点赞
评论