简体   繁体   中英

nested for loop in python to do the following

I am trying to write an equation for a nested for loop. i am getting an error and am trying to understand how to write it?

ysub =  GPA
0   0.4509999999999996
1   -0.04900000000000038
2   -0.5490000000000004
3   0.20099999999999962
4   -0.4490000000000003
5   0.19099999999999984
6   0.4509999999999996
7   -0.5490000000000004
8   0.25099999999999945
9   0.05099999999999971

Code:

i = range (1,10,1)
m = range (1,10,1)
 RMSEtest = ((1/m)*(ysub[i]^2))

we have to calculate RMSEtest (sum of values from m = 1 to 10) for each of the ysub values.

first of all that is not a for-loop. You really don't need to add a third parameter to the range because the default is 1 . Your question is unclear about what you need the for-loop to do and the code doesn't help.
For nested for-loops, you want something like:

for x in I:
    for y in m:
        do_something(x, y)

There were a lot of mistakes

  1. you cannot divide integer with a range object as told by @ForceBru.

  2. You don't need 2 loops for your work

  3. To raise the power of an integer or a float, '**' is used instead of '^'

     ysub = [0.4509999999999996, -0.04900000000000038,-0.5490000000000004,0.20099999999999962,-0.4490000000000003, 0.19099999999999984, 0.4509999999999996, -0.549000000000000, 0.25099999999999945, 0.05099999999999971]

Code:

for m in range (1,10):
    RMSEtest = ((1/m)*(ysub[m]**2))

From your code, I think this is what you wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM