【问题标题】:Python: add a timestamp to each object in the listPython:为列表中的每个对象添加时间戳
【发布时间】:2013-11-15 08:04:39
【问题描述】:

我有一个对象集合,我想为每个对象添加一个时间戳:

t = time.utcnow()  
for x in objects:
    x['created_at'] = t

有没有办法做到这一点?

【问题讨论】:

  • 您想要 1 班轮解决方案?
  • 您想为所有人添加相同的时间戳吗?一次(一口气)?
  • @AshishNitinPatil,是的,这就是我想要的

标签: python python-2.7 collections


【解决方案1】:

不完全漂亮(假设x 是一本字典):

t = time.utcnow()
map(lambda x: x.update({'created_at': t}), objects)

【讨论】:

  • update 返回None。因此,我们将获得None 的列表
  • 然后将映射分配给objects?
  • @Antonio map() 返回None 对象列表,但原始objects 列表将更新字典。
猜你喜欢
  • 1970-01-01
  • 2015-02-16
  • 2018-04-02
  • 1970-01-01
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
  • 2018-04-29
  • 2018-09-19
相关资源
最近更新 更多