我在 Python 2.6 中有一个元组,详细信息如下:
mylist = [
['20120903', 'melon', 'shelf1', '05:31', '08:01'],
['20120903', 'melon', 'shelf1', '05:31', '14:01'],
['20120903', 'melon', 'shelf1', '05:31', '23:59'],
['20120903', 'melon', 'shelf1', '10:18', '14:01'],
['20120903', 'melon', 'shelf1', '10:18', '23:59'],
['20120904', 'melon', 'shelf1', '00:00', '14:02'],
['20120904', 'melon', 'shelf1', '05:32', '14:02'],
['20120903', 'apple', 'shelf5', '05:34', '14:02'],
['20120903', 'apple', 'shelf5', '05:34', '23:59'],
['20120904', 'apple', 'shelf5', '00:00', '14:02'],
['20120904', 'apple', 'shelf5', '05:33', '14:02']]
我想得到如下结果(当第 0、1、2、3 列相同时,取第 4 列的最小值 + 当第 0、1、2、4 列相同时,取最大值第 3 列):
result = [
['20120903', 'melon', 'shelf1', '05:31', '08:01'],
['20120903', 'melon', 'shelf1', '10:18', '14:01'],
['20120904', 'melon', 'shelf1', '05:32', '14:02'],
['20120903', 'apple', 'shelf5', '05:34', '14:02'],
['20120904', 'apple', 'shelf5', '05:33', '14:02']]
现在感谢 Ashwini Chaudhary,我修改了一些他的代码,现在它看起来像下面这样:
from itertools import groupby
mylist = [
['20120903', 'melon', 'shelf1', '05:31', '08:01'],
['20120903', 'melon', 'shelf1', '05:31', '14:01'],
['20120903', 'melon', 'shelf1', '05:31', '23:59'],
['20120903', 'melon', 'shelf1', '10:18', '14:01'],
['20120903', 'melon', 'shelf1', '10:18', '23:59'],
['20120904', 'melon', 'shelf1', '00:00', '14:02'],
['20120904', 'melon', 'shelf1', '05:32', '14:02'],
['20120903', 'apple', 'shelf5', '05:34', '14:02'],
['20120903', 'apple', 'shelf5', '05:34', '23:59'],
['20120904', 'apple', 'shelf5', '00:00', '14:02'],
['20120904', 'apple', 'shelf5', '05:33', '14:02']]
step1 = []
for k1, g1 in groupby(mylist, key=lambda x1: (x1[0], x1[1], x1[2], x1[3])):
step1.append((min(g1, key=lambda x1: map(int, x1[4].split(':')))))
step2 = []
for k2, g2 in groupby(step1, key=lambda x2: (x2[0], x2[1], x2[2], x2[4])):
step2.append((max(g2, key=lambda x2: map(int, x2[3].split(':')))))
for result in step2:
print result