Python列表理解语法错误

Python列表理解语法错误,python,list-comprehension,Python,List Comprehension,使用for循环,下面的python代码可以工作 for item in results: item ['currentservertime'] = int(time.time()) 我想做一个列表,但它的理解。因此,我尝试了以下操作,但在= item['currentservertime'] = int(time.time()) for item in results 我哪里出错了?列表理解在这里不起作用,因为您在任何时候都不会创建列表-您正在更改各种词典中的值。如果您的原始代码为以

使用for循环,下面的python代码可以工作

for item in results:
    item ['currentservertime'] = int(time.time())
我想做一个列表,但它的理解。因此,我尝试了以下操作,但在=

item['currentservertime'] = int(time.time()) for item in results

我哪里出错了?

列表理解在这里不起作用,因为您在任何时候都不会创建列表-您正在更改各种词典中的值。如果您的原始代码为以下格式,列表理解将是正确的工具:

currentservertime = []
for item in results:
    currentservertime.append(int(time.time())
这将转化为列表理解:

currentservertime = [int(time.time()) for item in results]

目前,您现有的循环是实现您正在做的事情的最清晰、最直接的方式。

列表理解在这里不起作用,因为您在任何时候都没有构建列表-您正在更改各种字典中的值。如果您的原始代码为以下格式,列表理解将是正确的工具:

currentservertime = []
for item in results:
    currentservertime.append(int(time.time())
[i.update({'currentservertime': int(time.time())}) for i in results]
这将转化为列表理解:

currentservertime = [int(time.time()) for item in results]

目前,您现有的循环是实现您正在做的事情的最清晰和最直接的方式。

ps我应该提到我只是开始pythonth,这不是LCs的工作方式,也不是LCs的用途。ps我应该提到我只是开始pythonth,这不是LCs的工作方式,也不是LCs的用途。
[i.update({'currentservertime': int(time.time())}) for i in results]