我尝试在有条件的情况下在一行中使用两个for循环
但无法识别i值
pack = [a,b,c]
answer =[x== y for x,y in zip(i * int(len(answers)/len(i)) if len(answers)%len(i) ==0 else i * int(len(answers)/len(i)+1),answers) for i in pack]
并返回消息
----> 7个答案= [x == y表示zip(i * int(len(answers)/ len(i))中的x,y(如果len(answers)%len(i)== 0否则我* int(len(answers / len(i)+1),answers)in in pack] 8 9个答案= [i的i + 1,如果j == max(answer),则j在枚举(answer)中
NameError:名称'i'未定义
答案 0 :(得分:0)
'Single Line Nested For Loops'
我相信这个链接问题的答案将对嵌套循环部分有所帮助。对于带有“ i”变量的名称错误,您尝试在定义的范围之外使用该变量。
您基本上有:
for x, y in zip
{
//where you are trying to use the variable i before it exists
}
for i in packs
{
}