元组的原始列表是:
[ (datetime.date(2014, 8, 3), datetime.date(2014, 8, 16)), (datetime.date(2014, 7, 20), datetime.date(2014, 8, 2))]
如何在一行中转换为[('2014-08-03','2014-08-16'),('2014-07-20','2014-08-02')]
。
答案 0 :(得分:1)
a = [(datetime.date(2014, 8, 3), datetime.date(2014, 8, 16)), (datetime.date(2014, 7, 20), datetime.date(2014, 8, 2))]
[tuple(map(str, i)) for i in a]
答案 1 :(得分:1)
更易读的方式(也许是因为没有嵌套列表理解)是:
import datetime
date_list = [(datetime.date(2014, 8, 3), datetime.date(2014, 8, 16)),
(datetime.date(2014, 7, 20), datetime.date(2014, 8, 2))]
date_list_strings = [(str(start), str(end)) for start, end in date_list]
>>> date_list_strings
[('2014-08-03', '2014-08-16'), ('2014-07-20', '2014-08-02')]