如何在一年中的几个月分配数字?蟒蛇

时间:2015-04-08 03:43:13

标签: python date datetime

我想分配

1->March
2->April
3->May
11->January
12->February

我怎么能在python中做到这一点?

2 个答案:

答案 0 :(得分:1)

>>> from datetime import datetime
>>> {datetime(year=2015, month=x, day=1).strftime('%B'): 1 + (x - 3) % 12 for x in range(1, 13)}
{'April': 2,
 'August': 6,
 'December': 10,
 'February': 12,
 'January': 11,
 'July': 5,
 'June': 4,
 'March': 1,
 'May': 3,
 'November': 9,
 'October': 8,
 'September': 7}

答案 1 :(得分:1)

import calendar
{1 + (i - 3) % 12:calendar.month_name[i] for i in range(1, 13)}

它的输出是

{1: 'March',
 2: 'April',
 3: 'May',
 4: 'June',
 5: 'July',
 6: 'August',
 7: 'September',
 8: 'October',
 9: 'November',
 10: 'December',
 11: 'January',
 12: 'February'}