如何在python中更改时间格式? [英] How to change time formats in python?
本文介绍了如何在python中更改时间格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何获得以下列表
[datetime.date(2011, 4, 1), datetime.date(2011, 4, 8), datetime.date(2011, 4, 16), datetime.date(2011, 5, 21)]
作为
['2011-04','2011-05']
它不仅转换为字符串而且删除重复
It not only convert to string but remove dups
推荐答案
dates = [date.strftime('%Y-%m') for date in dates]
您可以通过将列表转换为集合然后再转换回列表来删除重复项.
You can remove duplicates by converting the list to a set and then back to a list.
dates = list(set(dates))
然后将两种方法结合起来,一步完成.
Then combine both methods to do it all in one step.
dates = list(set([date.strftime('%Y-%m') for date in dates]))
这篇关于如何在python中更改时间格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文