在Python中使用嵌套数组和for循环运行平均值而不使用numpy

在Python中使用嵌套数组和for循环运行平均值而不使用numpy,python,for-loop,grouping,Python,For Loop,Grouping,我试图找到一种方法,使用for循环获得嵌套列表中元素的最终运行平均值,但我一直在思考如何进行。下面我有一些示例数据,其中每个子列表的元素[0]是要分组的id标记,元素[1]是要求和的值,元素[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(

我试图找到一种方法,使用for循环获得嵌套列表中元素的最终运行平均值,但我一直在思考如何进行。下面我有一些示例数据,其中每个子列表的元素[0]是要分组的id标记,元素[1]是要求和的值,元素[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:

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])