1

experts i want to get the sum of the elements generated by for loop(x) in python.I wrote a programme for that but i am not getting the sum of x values.please help.

import numpy as np

f=np.arange(1,2,1)

for i in f:
    #print(i)
    for m in np.arange(1,6,1):
        x=((4.6)*(m*2)*(5)**2)*(i)/62
        print(x)
        #print(np.sum(x))
  • It looks like you were printing out intermediate values as you were writing this code to get a better sense of what was happening. Well done. – quamrana Dec 06 '20 at 11:27

3 Answers3

2

As I suppose, you want to compute the sum of all generated values.

The first, "classic" and not very efficient method is to create a list, append each generaed element to this list and then compute the sum of this list:

f = np.arange(1,2)
tbl = []
for i in f:
    for m in np.arange(1,6):
        x = ((4.6)*(m*2)*(5)**2)*(i)/62
        print(f'{i}, {m}: {x:.4f}')
        tbl.append(x)
print(sum(tbl))

The result is:

1, 1: 3.7097
1, 2: 7.4194
1, 3: 11.1290
1, 4: 14.8387
1, 5: 18.5484
55.64516129032258

But a more efficient and numpythonic option is to use np.fromfunction method to generate the whole Numpy array in one go and then compute its sum:

arr = np.fromfunction(lambda i, m: 4.6 * m * 2 * 5 ** 2 * i / 62, (2, 6))
print(arr)
print(arr.sum())

Note that I removed unncessary parentheses from your code.

Your function can actually be yet more simplified to:

lambda i, m: i * m * 9.2 * 5 ** 2 / 62

This time the result is:

[[ 0.          0.          0.          0.          0.          0.        ]
 [ 0.          3.70967742  7.41935484 11.12903226 14.83870968 18.5483871 ]]
55.64516129032258

Edit

From one of other answers I see that you may be interested in a sequence of cumulative sums of your values.

After you generate arr, you can generate such a sequence with:

np.cumsum(arr[1:,1:])

even for more than one rows (values of i). Test it e.g. for shape of (3, 6).

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
1

Declare and initialize a variable outside the loop and add x to that variable.

import numpy as np

f=np.arange(1,5,1)

for i in f:
    sum = 0
    for m in np.arange(1,6,1):
        x=((4.6)*(m*2)*(5)**2)*(i)/62
        sum += x
    print(i, sum)

Output:

1 55.64516129032258
2 111.29032258064515
3 166.93548387096774
4 222.5806451612903
  • 1
    btw Don't use `sum` as a variable name. It is a built-in. – quamrana Dec 06 '20 at 11:40
  • sum is not a keyword in python. Following code will return False, let me know if I am missing something #### import keyword print('sum' in keyword.kwlist) – Kalyan Saha Dec 06 '20 at 11:45
  • 1
    @glkshu Please tell what is your excepted output in both cases np.arange(1,2,1) and np.arange(1,5,1) – Kalyan Saha Dec 06 '20 at 11:50
  • 1
    sum is not a keyword in python. It is a built-in, just like enumerate, list or zip. – quamrana Dec 06 '20 at 11:55
  • Ok. got your point. Updated code. Please check if this works – Kalyan Saha Dec 06 '20 at 11:58
  • @glkshu This may help [Plot with matplotlib](https://stackoverflow.com/questions/21519203/plotting-a-list-of-x-y-coordinates-in-python-matplotlib) But for this, you will have to add all `sum` in a list. – Kalyan Saha Dec 06 '20 at 12:25
0

Just append your x to a list and use sum().

import numpy as np



f=np.arange(1,2,1)

for i in f:
    items = []
    for m in np.arange(1,6,1):
        x=((4.6)*(m*2)*(5)**2)*(i)/62
        items.append(x)
    print(sum(items))
quamrana
  • 37,849
  • 12
  • 53
  • 71