运行 在 Python 中使用嵌套数组和 for 循环的平均值,没有 numpy
Running Average using nested arrays and for-loops in Python without numpy
我正在尝试找到一种方法来使用 for 循环获得嵌套列表中元素的最终 运行 平均值,但我对如何去做感到困惑。我在下面有一些示例数据,其中每个子列表的 element[0] 是要分组的 id 标记,element[1] 是要求和的值,而 element[2] 充当计数器
listA = [[u001,4,1],[u002,6,1],[u003,3,1],[u001,12,1],[u002,1,1]
listB = []
for data in listA:
if data[0] not in listB:
listB.append(data)
else: # essentailly if the value is in listB then do:
listB[1] += data[1] #but this won't work because listB[1] is a list, and data[1] is a value
# i need to find a way to append the value data in listB WHERE listB[0] == data[0]
AND
listB[2] += 1 #at that specific location
例如 pf 每个 for 循环传递:
listA:[[u001,4,1],[u002,6,1],[u003,3,1],[u001,12,1],[u002,1,1]
listB:[]
listB:[[u001,4,1]]
listB:[[u001,4,1],[u002,6,1]]
listB:[[u001,4,1],[u002,6,1],[u003,3,1]]
listB:[[u001,16,2],[u002,6,1],[u003,3,1]]
listB:[[u001,16,2],[u002,7,2],[u003,3,1]] #final desired result
如果你愿意使用字典,你可以这样做:
listA = [['u001',4,1],['u002',6,1],['u003',3,1],['u001',12,1],['u002',1,1]]
listB = {}
for a in listA:
listB[a[0]] = [listB.get(a[0],[0,0])[0] + a[1],listB.get(a[0],[0,0])[1] + a[2]]
我发布的解决方案的问题是时间复杂度为 O(n²)。如果您知道您的数据不是那么大,那么使用它可能会很好。
listA = [['u001',4,1],['u002',6,1],['u003',3,1],['u001',12,1],['u002',1,1]]
listB = []
for data in listA:
found = False
for elem in listB:
if elem[0] == data[0]:
elem[2] += 1
elem[1] += data[1]
found = True
break
if not found:
listB.append(data)
另一个使用 itertools.groupby
的解决方案:
from operator import itemgetter
from itertools import groupby
listA = [['u001', 4, 1], ['u002', 6, 1],
['u003', 3, 1], ['u001', 12, 1],
['u002', 1, 1]]
listA.sort(key=itemgetter(0))
listB = []
for k, g in groupby(listA, key=itemgetter(0)):
suma = 0
counta = 0
for x, y, z in g:
suma += y
counta += 1
listB.append([x, suma, counta])