我想根据循环中发生的变化来更改“for循环”的步骤。例如:
for i in np.arange(0,l00):
i=i*10
在提到的代码中循环运行100次,而我希望i*10对循环产生影响并且它运行10次!你能指导我吗?
事实上,这只是一个简单的例子,但主要问题是改变循环中的步骤。像这样:
data = pd.read_csv('/home/Desktop/dataset.txt')
for i in np.arange(0,l00):
if data[i,0]==5:
i=i*5
elif data[i,0]==10:
i=i*10
else:
i=i*20
最佳答案
RTFM numpy.arange ,这是第三个参数:
numpy.arange([start, ]stop, [step, ]dtype=None)
Parameters:
start : number, optional
Start of interval. The interval includes this value. The default start value is 0.
stop : number
End of interval. The interval does not include this value, except in some cases where step is not an integer and floating point round-off affects the length of out.
step : number, optional <<------- HERE
Spacing between values. For any output out, this is the distance between two adjacent values, out[i+1] - out[i]. The default step size is 1. If step is specified, start must also be given.
关于python - python中 "for loop"的步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33331233/