【问题标题】:How to find a min value of a row in a specified column in a tuple - Python 2.6? [closed]如何在元组的指定列中找到行的最小值 - Python 2.6? [关闭]
【发布时间】:2013-07-02 09:05:15
【问题描述】:

我在 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 列相同时,取第三列的最大值):

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

【问题讨论】:

  • 什么决定了mylist(新的)中的内容?
  • 哪一个?输入列表还是输出列表?
  • 如果您想知道 mylist 中的内容,那么我只能告诉结构中始终存在随机数据:日期、水果、货架、start_growth_time、end_growth_time
  • 请重新打开我的问题

标签: python list nested tuples min


【解决方案1】:

我猜你需要这样的东西:

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

【讨论】:

  • 你是我的上帝!你怎么知道如何找到参考点?非常感谢!效果很好!
  • @constantine 这几乎可以猜到:)。如果对您有用,请随时 accept the answer
猜你喜欢
  • 2022-10-17
  • 1970-01-01
  • 2021-06-15
  • 2022-12-10
  • 1970-01-01
  • 2013-01-26
  • 1970-01-01
  • 2013-12-24
  • 1970-01-01
相关资源
最近更新 更多