我在Python 2.6元組,在下面詳細說明:如何查找元組中指定列中行的最小值 - 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-RD柱的最大值):
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']]
現在由於阿什維尼喬杜裏,我修改有點他的代碼,現在它如下所示:
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
是什麼決定了什麼是'mylist'(新一)? – TerryA
哪一個?輸入列表或輸出列表? – constantine
如果你想知道mylist是什麼,所以我只能告訴結構中有一個隨機數據:date,fruit,shelf,start_growth_time,end_growth_time – constantine