Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个类似的清单
A= [(1,0,10),(1,10,20), (1,20,30), (2,5,25), (2,5,50), (2,50,100)]
我需要根据 A 中每个元组的第一个成员对其进行分组,然后是每个组的最小值和最大值。输出是这样的
B = [(1,0,30),(2,5,100)]
>>> from operator import itemgetter >>> from itertools import groupby def solve(lis): for k, g in groupby(lis, key=itemgetter(0)): lis = [y for x in g for y in x[1:]] yield (k, min(lis), max(lis)) ... >>> list(solve(A)) [(1, 0, 30), (2, 5, 100)]