numpy - Create new column for data after each iteration of a for loop in python -


using next code want put results in columns differents values of m

import numpy np  m in np.arange(0, 4, 1):    n in np.arange(1, 4, 1):        coef = 2*m/n        print coef 

the results of is:

0 0 0 2 1 0 4 2 1 6 3 2 

i want this

0 0 0 2 1 0 4 2 1 6 3 2 

or directly column, sum of values of rows

0 + 0 + 0 = 0 2 + 1 + 0 = 3 4 + 2 + 1 = 7 6 + 3 + 2 = 11 

add comma after print don't add newline , print outside inner loop separate each line:

import numpy np m in np.arange(0, 4, 1):    n in np.arange(1, 4, 1):        coef = 2*m/n        print coef,    print  

which give you:

0 0 0 2 1 0 4 2 1 6 3 2 

or using print function:

from __future__ import print_function   import numpy np  m in np.arange(0, 4, 1):     print(*(2 * m / n n in np.arange(1, 4, 1))) 

to sum, can use builtin sum function using generator expression in inner loop:

import numpy np  m in np.arange(0, 4, 1):     print(sum(2 * m / n n in np.arange(1, 4, 1))) 

which give you:

0 3 7 11 

for completeness, equivalent print_function code first example setting end=" ":

from __future__ import print_function  import numpy np m in np.arange(0, 4, 1):    n in np.arange(1, 4, 1):        coef = 2*m/n        print(coef, end=" ")    print()