2013-07-04 52 views
-5

我在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 
+3

是什麼決定了什麼是'mylist'(新一)? – TerryA

+0

哪一個?輸入列表或輸出列表? – constantine

+0

如果你想知道mylist是什麼,所以我只能告訴結構中有一個隨機數據:date,fruit,shelf,start_growth_time,end_growth_time – constantine

回答

0

我想你需要的東西是這樣的:

>>> from itertools import groupby 
#filter items that contain '00:00' 
>>> mylist = [x for x in mylist if x[-2] != '00:00' ] 

#now group lists based on the the second last item 
for k,g in groupby(mylist, key = lambda x :x [-2]): 
    #find the min among the grouped lists based on the last item 
    minn = min(g, key = lambda x : map(int,x[-1].split(':'))) 
    print minn 
...  
['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 
def solve(lis): 
    mylist = [x for x in lis if x[-2] != '00:00' ] 
    for k,g in groupby(mylist, key = lambda x :x [-2]): 
      #find the min among the grouped lists based on the last item 
      minn = min(g, key = lambda x : map(int,x[-1].split(':'))) 
      yield minn 
...   
>>> list(solve(mylist)) 
[['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']] 
+0

你是我的G.O.D.!你怎麼知道如何找到參考點?非常感謝!它很棒! – constantine

+0

@constantine這是非常可猜測的:)。如果它適合你,請隨時[接受答案](http://meta.stackexchange.com/questions/5234/how-does-paccepting-an-answer-work/5235#5235)。 –

相關問題