Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
72 views
in Technique[技术] by (71.8m points)

python - different increment in range in for-loop pandas

I have a loop code as below:

min = 1
max = 300
inc = 10

for j in range (min,max+inc,inc):
    print(j)

The increment is 10 from min until max. Is there any way that I can make the increment change on certain range, for example from range 1-100 the increment is 10, 101-200 the increment is 20 and 201-300 the increment is 30?

question from:https://stackoverflow.com/questions/66060407/different-increment-in-range-in-for-loop-pandas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of a for loop you can use a while loop:

min = 1
max = 300
inc = 10

while min <= max+inc:
    print(min)

    if 100 < min <= 200: #define your increment-conditions here
        inc = 20
    elif 200 < min <= 300:
        inc = 30

    min += inc

output:

1
11
21
31
41
51
61
71
81
91
101
121
141
161
181
201
231
261
291
321

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...