尝试在python 3.7中运行以下代码:
date_list = [datetime.datetime.strptime(d['date'], '%Y-%m-%d') for d in date_settings and d['date'] != None]
这是由此产生的错误,有人可以向我说明为什么这个列表理解看不到自己的变量吗?
date_list = [datetime.datetime.strptime(d['date'], '%Y-%m-%d') for d in date_settings and d['date'] != None]
NameError: name 'd' is not defined
这是输入数组:
[{'date': '2020-05-08', 'changed_at': '2020-05-07T20:35:07.854Z'}, {'date': '2020-05-09', 'changed_at': '2020-05-07T20:35:40.604Z'}, {'date': '2020-05-10', 'changed_at': '2020-05-07T20:35:42.936Z'}]
内心的理解,你在哪里写
you should have
if
, notand
.正如您目前所掌握的那样,理解力正在试图理解该表达
as something that
d
should iterate through, which is why it doesn't know whatd
is supposed to mean here.