Python: 如何对列表中的相似列表进行分组并取平均值?
Python: how to group similar lists inside lists with tolerance and take average?
输入--> a = [[297, 151, 320], [293, 151, 305], [296, 151, 320], [295, 162, 306], [ 297, 160, 309], [300, 158, 321]
I have a list inside a list. I need to group by the third element in the list a[i][2] with tolerance + or - 5
output_1--> a = [[[297, 151, 320], [293, 151, 318], [296, 151, 321]], [[295, 162 , 306], [297, 160, 309], [300, 158, 305]]
Later I need to take an average of each subgroup such as [297, 151, 320], [293, 151, 318], [296, 151, 321] = [(297+293+296)/3,(151+151+151)/3,(320+318+321)/3] simillarly for the next group
final output
final_output--> a=[[[295,151,320]],[[297,160,307]]]
有人可以帮忙吗?
一种方法是创建一个生成器函数来进行公差分组。此函数假定列表已排序,因此您需要传入一个已排序的列表,或修改它以在函数中进行排序。我相信有人会找到使用 itertools.groupby
执行此操作的方法。
def groupby_tolerance(lst, tolerance):
result = [lst[0]]
for prev, curr in zip(lst, lst[1:]):
if curr[2] - prev[2] > tolerance:
yield result
result = []
result.append(curr)
yield result
然后在您的排序列表中调用此函数(按第 3 项):
from operator import itemgetter
a = [[297, 151, 320], [293, 151, 305], [296, 151, 320], [295, 162, 306], [297, 160, 309], [300, 158, 321]]
grouped = groupby_tolerance(sorted(a, key=itemgetter(2)), 5)
给出分组:
[[[293, 151, 305], [295, 162, 306], [297, 160, 309]], [[297, 151, 320], [296, 151, 320], [300, 158, 321]]]
然后就可以压缩对应的元素,并计算平均值:
from statistics import mean
averages = [[mean(x) for x in zip(*group)] for group in grouped]
print(averages)
平均值:
[[295, 157.66666666666666, 306.6666666666667], [297.6666666666667, 153.33333333333334, 320.3333333333333]]
对不起,我没有得到你想要的。因为每个列表的值都不一致。但是,你的意思是这样的吗?
a = [[297, 151, 320], [293, 151, 305], [296, 151, 320], [295, 162, 306], [297, 160, 309], [300, 158, 321]]
b = []
i = -1
for x in range(len(a)):
if (x%3) == 0:
b.append([])
i += 1
ave = int(sum(a[x])/len(a[x]))
b[i].append(ave)
print(b)
输出:
[[256, 249, 255], [254, 255, 259]]
输入--> a = [[297, 151, 320], [293, 151, 305], [296, 151, 320], [295, 162, 306], [ 297, 160, 309], [300, 158, 321]
I have a list inside a list. I need to group by the third element in the list a[i][2] with tolerance + or - 5
output_1--> a = [[[297, 151, 320], [293, 151, 318], [296, 151, 321]], [[295, 162 , 306], [297, 160, 309], [300, 158, 305]]
Later I need to take an average of each subgroup such as [297, 151, 320], [293, 151, 318], [296, 151, 321] = [(297+293+296)/3,(151+151+151)/3,(320+318+321)/3] simillarly for the next group
final output
final_output--> a=[[[295,151,320]],[[297,160,307]]]
有人可以帮忙吗?
一种方法是创建一个生成器函数来进行公差分组。此函数假定列表已排序,因此您需要传入一个已排序的列表,或修改它以在函数中进行排序。我相信有人会找到使用 itertools.groupby
执行此操作的方法。
def groupby_tolerance(lst, tolerance):
result = [lst[0]]
for prev, curr in zip(lst, lst[1:]):
if curr[2] - prev[2] > tolerance:
yield result
result = []
result.append(curr)
yield result
然后在您的排序列表中调用此函数(按第 3 项):
from operator import itemgetter
a = [[297, 151, 320], [293, 151, 305], [296, 151, 320], [295, 162, 306], [297, 160, 309], [300, 158, 321]]
grouped = groupby_tolerance(sorted(a, key=itemgetter(2)), 5)
给出分组:
[[[293, 151, 305], [295, 162, 306], [297, 160, 309]], [[297, 151, 320], [296, 151, 320], [300, 158, 321]]]
然后就可以压缩对应的元素,并计算平均值:
from statistics import mean
averages = [[mean(x) for x in zip(*group)] for group in grouped]
print(averages)
平均值:
[[295, 157.66666666666666, 306.6666666666667], [297.6666666666667, 153.33333333333334, 320.3333333333333]]
对不起,我没有得到你想要的。因为每个列表的值都不一致。但是,你的意思是这样的吗?
a = [[297, 151, 320], [293, 151, 305], [296, 151, 320], [295, 162, 306], [297, 160, 309], [300, 158, 321]]
b = []
i = -1
for x in range(len(a)):
if (x%3) == 0:
b.append([])
i += 1
ave = int(sum(a[x])/len(a[x]))
b[i].append(ave)
print(b)
输出:
[[256, 249, 255], [254, 255, 259]]