比较字典中列表中任意数量的日期

2024-09-27 09:37:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一本这样的日期字典(这里的键与其他工作相关,需要考虑):

{2: ['8-12-2012', '9-12-2012', '7-12-2012],
 5: ['10-12-2012', '11-12-2012'],
 7: ['13-12-2012']}

现在,我想在每个列表中找出最早的日期。最后,我需要找出最早的日期,返回那个日期和钥匙。你知道吗

如果我手动执行此处尝试执行的操作:

**key 2**, `7-12-2012` is the earliest.
**key 5**, `10-12-2012` is the earliest.
**key 7**, `13-12-2012` is the earliest.

2012年7月12日是最早的日期,所以我应该返回2。你知道吗

注意事项:

  1. 字典中的数据是在运行时动态创建的。你知道吗
  2. 词典中的列表不是固定长度的。你知道吗

这是我尝试过的,但只比较了两个日期:

...
...
# this value would be dynamically set during runtime
expiryDates[item] = {2: ['8-12-2012', '9-12-2012', '7-12-2012], 5: ['10-12-2012', '11-12-2012'], 7: ['13-12-2012']}

datesInBox = []
dict_earliest_expiration = defaultdict(list)

for n in expiryDates:
    datesInBox = expiryDates[n] # when n = 2; datesInBox = ['8-12-2012', '9-12-2012']
    d1 = time.strptime(datesInBox[0], "%d-%m-%Y")
    d2 = time.strptime(datesInBox[1], "%d-%m-%Y")
    if d1 < d2:
        dict_earliest_expiration[n] = d1
    else:
        dict_earliest_expiration[n] = d2

任何帮助都将不胜感激。你知道吗


Tags: thekey列表字典timeisdictd2
3条回答

我会慢慢来,让你看看这个过程。首先,反转指令:以时间为键,前一个键为值:

exp = {2: ['8-12-2012', '9-12-2012', '7-12-2012'], 5: ['10-12-2012', '11-12-2012'], 7: ['13-12-2012']}
rev = []

for key, val_list in exp.items():
    for val in val_list:
        rev[time.strptime(val, "%d-%m-%Y")] = key

清理表示,rev现在

{
    time.struct_time(tm_year=2012, tm_mon=12, tm_mday=13): 7, 
    time.struct_time(tm_year=2012, tm_mon=12, tm_mday= 8): 2, 
    time.struct_time(tm_year=2012, tm_mon=12, tm_mday= 7): 2, 
    time.struct_time(tm_year=2012, tm_mon=12, tm_mday= 9): 2, 
    time.struct_time(tm_year=2012, tm_mon=12, tm_mday=11): 5, 
    time.struct_time(tm_year=2012, tm_mon=12, tm_mday=10): 5
}

现在只需最早打印钥匙:

>>> rev[min(rev)]
2

如果你愿意,你可以把它折叠成一个听写理解和一个琐碎的调用。你知道吗

您可以将所有字符串转换为日期,然后使用min函数:

import time

data = {2: ['8-12-2012', '9-12-2012', '7-12-2012'], 5: ['10-12-2012', '11-12-2012'], 7: ['13-12-2012']}
d2 = {k: [time.strptime(e, "%d-%m-%Y") for e in v] for k, v in data.items()}
print(min(d2, key=lambda e: min(d2[e])))

输出

2

或者,您可以预先计算字典中每个键的最小值:

data = {2: ['8-12-2012', '9-12-2012', '7-12-2012'], 5: ['10-12-2012', '11-12-2012'], 7: ['13-12-2012']}
d2 = {k: min(time.strptime(e, "%d-%m-%Y") for e in v) for k, v in data.items()}
print(min(d2, key=lambda e: d2[e]))

输出

2

最后,您可以迭代键、值对,而不是迭代键:

data = {2: ['8-12-2012', '9-12-2012', '7-12-2012'], 5: ['10-12-2012', '11-12-2012'], 7: ['13-12-2012']}
d2 = {k: min(time.strptime(e, "%d-%m-%Y") for e in v) for k, v in data.items()}
print(min(d2.items(), key=lambda t: t[1])[0])

输出

2

反复使用dict或直接传递密钥。将列表转换为熊猫系列并排序

import pandas as pd
d={2: ['8-12-2012', '9-12-2012', '7-12-2012'], 5: ['10-12-2012', '11-12-2012'], 7: ['13-12-2012']}

for key,value in d.items():
    print(key,pd.to_datetime(pd.Series(value)).sort_values().iloc[0])

输出

(2, Timestamp('2012-07-12 00:00:00'))
(5, Timestamp('2012-10-12 00:00:00'))
(7, Timestamp('2012-12-13 00:00:00'))

如果你只关心日期

for key,value in d.items():
    print(key,pd.to_datetime(pd.Series(value)).dt.date.sort_values().iloc[0])

输出:

(2, datetime.date(2012, 7, 12))
(5, datetime.date(2012, 10, 12))
(7, datetime.date(2012, 12, 13))

根据给出的示例表示日期

for key,value in d.items():
    print('key: {}, Earliest Date: {} '.format(key,pd.to_datetime(pd.Series(value)).dt.date.sort_values().iloc[0].strftime("%m-%d-%Y")))

输出:

key: 2, Earliest Date: 07-12-2012 
key: 5, Earliest Date: 10-12-2012 
key: 7, Earliest Date: 12-13-2012 

相关问题 更多 >

    热门问题