简体   繁体   中英

For loop with two different indexes in python?

I have the following loop in java

double f,x,y;
int i;
for(i=0, f=0.01; i<100 && f<1.0; i++,f+=0.01)
{
     x=y*i+y*f;
     system.out.println("x = " +x,i , f);
}

But I would like to have 2 different indexes at once in it.

try this one

for i, j in zip(range(100), [(x/10) for x in range(100)]):
  #print (i, j)
  #your code

For this example, you can define f in terms of i :

for i in range(0, 100):
    f = (i + 1) / 100.0
    ...

You can use a while loop instead.

f = -1
i = 0
x = -1
y = -1
while (i < 100 and f < 1.0):
    x = y * i + y * f
    print('x = {} {} {}'.format(x, i, f))
    i += 1
    f += 0.01

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM