Niccolò
Niccolò

Reputation: 169

How to perform sum of elements iterated in Python loop

I'm really new to Python, and I'm facing troubles in understanding how its logic works. I'm trying to build a simple code that, given two lists, finds the difference between every number of the lists and returns the mean of the minimum differences.

Let's say I have this two lists:

list1=[1, 4, 10]
list2=[2, 3, 6]

with the code below, I can iterate the numbers of the first list with the numbers of the second:

for x in list1:

   diff=[abs(y-x) for y in list2]
   print (diff)

[1, 2, 5] # that is, 1-2, 1-3 and 1-6
[2, 1, 2] # that is, 4-2, 4-3 and 4-6
[8, 7, 4] # that is, 10-2, 10-3, 10-6

With the following code, I can find, for every iteration, the minimum difference:

for x in list1:

   diff=[abs(y-x) for y in list2]
   mindiff=min(int(s) for s in diff)

   print (mindiff)

1 
1 
4 

And I'm fine with that. Now I want to sum all the minimum differences and divide the sum by the number of the differences that I have calculated. Here comes the part that I don't get. In other words, how can I build a function that sums up all the iterations that the for loop has made? After that, (which, in this case would be 6 (1+1+4)) I could easily divide the sum of the differences for the maximum range of list2.

How can I do it?

Thank you in advance for your answers

Upvotes: 1

Views: 3569

Answers (3)

2016rshah
2016rshah

Reputation: 681

list1=[1, 4, 10]
list2=[2, 3, 6]

sumOfMinDiffs = 0 # Initialize the sum value to start out at zero

for x in list1:

   diff=[abs(y-x) for y in list2]
   mindiff=min(int(s) for s in diff)
   print (mindiff)

   sumOfMinDiffs += mindiff # Keep track of the sum


maximumRangeOfList2 = max(list2) - min(list2) # I think this is what you mean by "maximum range of list 2"

print(sumOfMinDiffs/maximumRangeOfList2) # Tah dah, this is your answer
# print(sumOfMinDiffs/len(list2)) # This is your answer if you just want to divide by the length of list2

The code above is meant to do the following:

  • Keep a sum variable outside of the loop and increment it on each loop
  • At the end of the for loop, calculate what the "maximum range of list 2" will be and store it in a variable
  • For your final answer just divide the sum variable by the range variable

Upvotes: 2

Kshitij Saraogi
Kshitij Saraogi

Reputation: 7609

The following code might help you solve the problem.

list1=[1, 4, 10]
list2=[2, 3, 6]

n = len(list2)
sum_min = 0

for x in list1:

    diff=[abs(y-x) for y in list2]
    mindiff=min(int(s) for s in diff)

    sum_min += mindiff  # iterating will keep on adding min of differences 1 + 1 + 4

    print (mindiff)

print(sum_min/n)  # mean minimum difference

I think in order to calculate the mean of minimum differences of the lists you need to divide the total sum of minimum differences by the number of differences in each set (i.e length of list2). If you want to modify the code, I think you have understood how to do it.

Upvotes: 1

Gerard Abello
Gerard Abello

Reputation: 676

I think you can achieve what you want with a variable outside the for loop, adding the value of each iteration and a variable counting the number of iterations.

list1=[1, 4, 10]
list2=[2, 3, 6]


min_sum = 0
n = 0
for x in list1:

    diff=[abs(y-x) for y in list2]
    mindiff=min(int(s) for s in diff)
    min_sum += mindiff
    n+=1

    print (mindiff)

print(min_sum/n)

Hope it helps

Upvotes: 2

Related Questions