python 中 for 循环内的变量
Variable within for loop in python
如果我有一个由 for 循环生成的变量,有没有办法从当前循环迭代中引用前一个循环迭代生成的同一个变量?这是我正在尝试做的伪代码:
for i in list:
var = generate(i)
do stuff with var
if(condition = true):
do stuff to the previously generated var
当然可以:
previous = None
for i in list:
var = generate(i)
...do stuff with var...
if ...condition...:
...do stuff with previous...
previous = var
注意:对于 previous is None
,...condition...
不应该是 True
。
这里有一些伪代码来实现这一点。
old = None
for i in list:
var = generate(i)
do_stuff(var)
if old is not None:
do_other_stuff(old)
old = var
如果我有一个由 for 循环生成的变量,有没有办法从当前循环迭代中引用前一个循环迭代生成的同一个变量?这是我正在尝试做的伪代码:
for i in list:
var = generate(i)
do stuff with var
if(condition = true):
do stuff to the previously generated var
当然可以:
previous = None
for i in list:
var = generate(i)
...do stuff with var...
if ...condition...:
...do stuff with previous...
previous = var
注意:对于 previous is None
,...condition...
不应该是 True
。
这里有一些伪代码来实现这一点。
old = None
for i in list:
var = generate(i)
do_stuff(var)
if old is not None:
do_other_stuff(old)
old = var