student = [['benny', 12.09], ['harry', 40.03], ['jenny', 56.03], ['garry', 56.33]]
for items in student:
for _ in range(len(items)):
if items[1] not in allscores:
allscores.append(items[1])
print allscores
上面代码的输出是
[12.09, 40.03, 56.03, 56.33]
正如预期的那样。
但是如果我将 2 个for循环转换为列表理解,
allscores = [items[1] for _ in range(len(items)) if items[1] not in allscores for items in student]
print allscores
我得到:
allscores = [items[1] for _ in range(len(items)) if items[1] not in allscores for items in student]
NameError: name 'items' is not defined
我的列表理解有什么问题?
长风秋雁
相关分类