【问题标题】:Python: convert list of dictionary keys containing timestamps into a list of stringsPython:将包含时间戳的字典键列表转换为字符串列表
【发布时间】:2021-12-19 00:52:12
【问题描述】:

我有以下列表:

[(Timestamp('2017-01-26 00:00:00'), -0.19),
 (Timestamp('2018-04-05 10:00:00'), -0.15)]

我想把键从时间戳改成字符串,得到如下列表

['2017-01-26 00:00:00',
 '2018-04-05 10:00:00']

这可以用一行代码完成吗?

谢谢。

【问题讨论】:

  • 查看strftime

标签: python pandas dictionary datetime timestamp


【解决方案1】:

使用理解:

lst = [(Timestamp('2017-01-26 00:00:00'), -0.19),
       (Timestamp('2018-04-05 10:00:00'), -0.15)]

dt = [str(t[0]) for t in lst]
print(dt)

# Output
['2017-01-26 00:00:00', '2018-04-05 10:00:00']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 2022-10-13
    • 1970-01-01
    相关资源
    最近更新 更多