找到对象矩阵中最大值的最佳pythonic方法

时间:2012-12-13 14:00:04

标签: python itertools

我有以下代码:

resdata = dict()
rows = result.rows.all()
for key, group in groupby(rows, lambda x: x.space):
    row = list()
    for item in group:
        cell = {
            'time': item.time,
            'value': item.value
        }
        row.append(cell)
    resdata[key] = row

示例resdata将是:

    resdata = [
    {
      "skl": "nn_skl:5608", 
      "cols": [
        {
          "value": 115.396956868, 
          "time": "2012-06-02 00:00:00"
        }, 
        {
          "value": 112.501399874, 
          "time": "2012-06-03 00:00:00"
        }, 
        {
          "value": 106.528068506, 
          "time": "2012-06-18 00:00:00"
        }
      ], 
      "len": 226
    }, 
    {
      "skl": "nn_skl:5609", 
      "cols": [
        {
          "value": 114.541167284, 
          "time": "2012-06-02 00:00:00"
        }, 
      ], 
      "len": 226
    }, 
    {
      "skl": "nn_skl:5610", 
      "cols": [
        {
          "value": 105.887267189, 
          "time": "2012-06-18 00:00:00"
        }
      ], 
      "len": 225
    }
]

我想要做的是获得最大值'和最长的时间'在所有细胞中。

1 个答案:

答案 0 :(得分:3)

假设您已使用json.loads或诸如此类的东西转换为Python对象,那么您需要以下内容:

max(b["time"] for b in a["cols"] for a in data)