0

if I have an array of dates:

["2012-1-1", "2012-1-3","2012-5-3"]

I want to be able to get a result list of:

["January 2012", "May 2012"]

What is the best way to do this?

4

2 回答 2

2
[datetime.datetime.strptime(d, '%Y-%m-%d').strftime("%B %Y") for d in my_list]
于 2013-02-21T18:03:43.320 回答
0

如果您想按日期时间排序并剔除唯一性,我将分三部分执行此操作:

dates = [datetime.datetime.strptime(d, '%Y-%m-%d') for d in my_list]
dates.sort()
string_dates = [d.strftime("%B %Y") for d in dates]
## now we'll cull the list of duplicates
import itertools
[ grouped[0] for grouped in itertools.groupby(string_dates) ]
于 2013-02-21T19:08:14.997 回答