[Solved] How to find a min value of a row in a specified column in a tuple – Python 2.6? [closed]


I guess you need something like this:

>>> 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']

To get a list of lists you can use a generator function:

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']]

2

solved How to find a min value of a row in a specified column in a tuple – Python 2.6? [closed]