Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I increment a list within a loop without a second loop?

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...

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading