0

日付の配列がある場合:

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

次の結果リストを取得できるようにしたい:

["January 2012", "May 2012"]

これを行うための最良の方法は何ですか?

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

日時で並べ替えて一意のカリングを行う場合は、これを3つの部分に分けて行います。

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 に答える