Home > OS >  How can I increment a list within a loop without a second loop?
How can I increment a list within a loop without a second loop?

Time:09-29

I am trying to loop through an array of month end dates t within my current loop without creating another for loop.

t=np.array([[30],[31],[31],[31],[28],[31],[31]])
n = 7
for i in range(0, n):
    print(i * t)

expected output :

0,31,62,93...

CodePudding user response:

Here loop is not necessary use:

a = t * np.arange(n)[:, None]
print (a)
[[  0]
 [ 31]
 [ 62]
 [ 93]
 [112]
 [155]
 [186]]

Or:

a = t[:, 0] * np.arange(n)
print (a)
[  0  31  62  93 112 155 186]

CodePudding user response:

You have to use i as an index for t.

t=np.array([[30],[31],[31],[31],[28],[31],[31]])
n = 7
for i in range(0, len(t)):
    print (i * t[i])
  • Related