Home > Back-end >  I want to give 3 values :360 ,3600,2486 to a single variable in Python
I want to give 3 values :360 ,3600,2486 to a single variable in Python

Time:10-12

I know it's a simple question but I need your help. I need to make a short code that the output is this :

0 460, 1 3600, 2 2486, 3 460 ,4 3600, 5 2486, 6 460 ....

I wrote this code but something it's missing...I need to to this 237 times to gives these values to the variable

a = 0
for i in range(238):
  a = 460
  print(i, a)
  a = 3600
  print(i, a)
  a = 2486
  print(i, a)

Because the output it's this :

enter image description here

CodePudding user response:

Put the values in a list and use % to get the value based on i index

vals = [460, 3600, 2486]
for i in range(238):
    print(i, vals[i % len(vals)])

Output

0 460
1 3600
2 2486
3 460
4 3600
5 2486
....

CodePudding user response:

The itertools module from the standard library can help here. You could write:

import itertools

g = itertools.cycle((4, 6, 8))

for i in range(238):
    print(i, next(g))

CodePudding user response:

From the obscure one-liners dept:

print(*[f'{i} {x},' for i,x in zip(range(238), itertools.cycle([460,3600,2486]))])

Output

0 460, 1 3600, 2 2486, 3 460, 4 3600, 5 2486, 6 460, 

(ok you need to import itertools too)

  • Related