如何在python中将元组转换为字符串

时间:2015-01-05 09:03:28

标签: python

元组的原始列表是:

[ (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')]

2 个答案:

答案 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')]