【问题标题】:Python: get maximum value object for unique keyPython:获取唯一键的最大值对象
【发布时间】:2015-06-03 09:05:33
【问题描述】:

我有以下物品清单:

[
    {'country' : 'India', 'date' : '18-Mar-14'},
    {'country' : 'India', 'date' : '18-Apr-14'},
    {'country' : 'India', 'date' : '18-May-14'},
    {'country' : 'Australia', 'date' : '18-Mar-14'},
    {'country' : 'Australia', 'date' : '18-Apr-14'},
    {'country' : 'Australia', 'date' : '18-May-14'},
    {'country' : 'China', 'date' : '18-Mar-14'},
    {'country' : 'China', 'date' : '18-Apr-14'},
    {'country' : 'China', 'date' : '18-May-14'}
]

如何仅获取包含每个国家/地区的最大日期值的项目,即对于每个国家/地区,它返回包含具有最大日期的国家/地区的项目。在这种情况下,结果列表将是:

[
    {'country' : 'India', 'date' : '18-May-14'},
    {'country' : 'Australia', 'date' : '18-May-14'},
    {'country' : 'China', 'date' : '18-May-14'},
]

【问题讨论】:

  • 你能澄清一下“最大的日期”吗?您的意思是“最近的”吗?
  • 按国家分组,然后从该组中取“最大日期”
  • 是最近的日期。因为我的数据量很大,所以最有效的方法是最复杂的。

标签: python algorithm list logic


【解决方案1】:

使用循环并跟踪迄今为止每个国家/地区发现的最大值。您必须将这些日期解析为 datetime 对象,以便轻松比较它们:

from datetime import datetime

max_dates = {}
for entry in list_of_dicts:
    date = datetime.strptime(entry['date'], '%d-%b-%y')
    country = entry['country']
    if country not in max_dates or date > max_dates[country][0]:
        max_dates[country] = (date, entry)

result = [entry for date, entry in max_dates.values()]

演示:

>>> from datetime import datetime
>>> list_of_dicts = [
...     {'country' : 'India', 'date' : '18-Mar-14'},
...     {'country' : 'India', 'date' : '18-Apr-14'},
...     {'country' : 'India', 'date' : '18-May-14'},
...     {'country' : 'Australia', 'date' : '18-Mar-14'},
...     {'country' : 'Australia', 'date' : '18-Apr-14'},
...     {'country' : 'Australia', 'date' : '18-May-14'},
...     {'country' : 'China', 'date' : '18-Mar-14'},
...     {'country' : 'China', 'date' : '18-Apr-14'},
...     {'country' : 'China', 'date' : '18-May-14'}
... ]
>>> max_dates = {}
>>> for entry in list_of_dicts:
...     date = datetime.strptime(entry['date'], '%d-%b-%y')
...     country = entry['country']
...     if country not in max_dates or date > max_dates[country][0]:
...         max_dates[country] = (date, entry)
... 
>>> [entry for date, entry in max_dates.values()]
[{'date': '18-May-14', 'country': 'China'}, {'date': '18-May-14', 'country': 'Australia'}, {'date': '18-May-14', 'country': 'India'}]

【讨论】:

  • 我收到此错误:ValueError:时间数据“%e-%b-%y”与格式“1-Mar-12”不匹配。知道为什么吗?
  • @Tarun:使用更新版本;我在第一次修订中混淆了strptime() 的论点。
【解决方案2】:

您可以将月份名称映射到从 1 到 12 的相应数字,然后将每个国家/地区的日期属性用 (-) 分割,并比较日、月和年的数字。

【讨论】:

    【解决方案3】:

    或者在一行中:

    from itertools import groupby
    from datetime import datetime
    
    [(x,max(y,key=lambda o:datetime.strptime(o['date'], '%d-%b-%y'))) for x,y in groupby(sorted(t, key=lambda o: o['country']), key=lambda o: o['country'])]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 2017-06-11
      相关资源
      最近更新 更多