【发布时间】:2020-09-28 07:28:38
【问题描述】:
If 语句中的 return 可以附加一个列表吗?
python 是否知道列表元素实际上是字典,并且“子列表”会迭代这些元素?
我尝试在 For 之前指定数据类型,但这没有帮助
If 语句实际上是在访问字典吗?
我的代码:
In[1]: restaurants = [fork_fig, frontier_restaurant]
In [2]: def open_restaurants(restaurants):
for sublsit in restaurants:
op=[]
if sublist.get('is_closed')==False:
op.append(sublist.get('name'))
通过他们的代码:
In [1]: len(open_restaurants(restaurants))
Out[2]: NameErrorTraceback (most recent call last)
<ipython-input-14-e78afc732a29> in <module>
----> 1 len(open_restaurants(restaurants)) # 1
<ipython-input-13-56ad484d6dd9> in open_restaurants(restaurants)
2 for sublsit in restaurants:
3 op=[]
----> 4 if sublist.get('is_closed')==False:
5 op.append(sublist.get('name'))
6
NameError: name 'sublist' is not defined
接下来:我认为这是因为我定义的列表没有填充正确的信息。
In[3]: open_restaurants(restaurants)[0]['name'] # 'Fork & Fig'
Out[4]:TypeErrorTraceback (most recent call last)
<ipython-input-15-bf5607d088f4> in <module>
----> 1 open_restaurants(restaurants)[0]['name'] # 'Fork & Fig'
TypeError: 'NoneType' object is not subscriptable
【问题讨论】:
标签: python-3.x for-loop if-statement typeerror nameerror