-5

我在 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
4

1 回答 1

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']]
于 2013-07-04T11:24:50.110 回答