gpt4 book ai didi

python - python中 "for loop"的步骤

转载 作者:太空宇宙 更新时间:2023-11-03 17:19:25 26 4
gpt4 key购买 nike

我想根据循环中发生的变化来更改“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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com