Python Forum
Calculate the sum of the differences inside tuple
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculate the sum of the differences inside tuple
#1
Hello everyone,

I would like to calculate the sum of the differences between each value for each tuple. For instance :

(4,7,9) would give 3 (7-4 = 3) and 2 (9-2). And the sum of these differences would be 5.

And after that, I would like to print only the tuples whith a sum superior to a value.

So, for instance, if I put that list of tuples :

[(4,8,10,13);
(8,11,14,15),
(9;16;18;19)]
And then, I put a condition to print only the tuples with a sum of differences inferior to 10, then it will return this result :

[(4,8,10,13);
(8,11,14,15)]
About the code, I am a little bit lost.

I have no idea how I can calculate the difference between each value of each tuple. Anyone could help me please ?
Reply
#2
itertools pairwise might be useful for this.

https://docs.python.org/3/library/itertools.html
Reply
#3
How about this:

list_1 = [
    (4,8,10,13),
    (8,11,14,15),
    (9,16,18,19)
    ]

for item in list_1:
    n1,n2,n3,n4 = item
    r1 = n2-n1
    r2 = n3-n2
    r3 = n4-n3
    r_sum = r1+r2+r3
    if r_sum < 10:
        print(item)
Output:
(4, 8, 10, 13) (8, 11, 14, 15)

Adding...

Your syntax is all over the place, which is leading to confusion.

As an example:

[(4,8,10,13);
(8,11,14,15),
(9;16;18;19)]
... this is not a valid List object

And:

(9;16;18;19)
... this is not a valid Tuple object.

Also in your post...

"(4,7,9) would give 3 (7-4 = 3) and 2 (9-2). And the sum of these differences would be 5."

... is not correct. I think you mean...

(4,7,9) would give 3 (7-4 = 3) and 2 (9-7 = 2). And the sum of these differences would be 5.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#4
Can the list ever decrease? Would [2, 7, 5] ever be valid? If so, is the difference in the second pair to be seen as 2 or -2?

If the list always increases or if you take the forward difference (which can be negative), then the sum of all your differences is just the difference between the last and the first value:

(4,8,10,13) => 13 - 4 = 5
(8,11,14,15) => 15 - 8 = 7
(9;16;18;19) => 19 - 9 = 10

Otherwise you're looking for the sum of the absolute differences.
Reply
#5
Great insight! Associative property for the win.
(4, 8, 10, 13)
8-4 + 10-8 + 13-10 == 13 + (10 - 10) + (8 - 8) - 4 == 13 - 4

(8, 4, 10, 13)
4-8 + 10-4 + 13-10 == 13 + (10 - 10) + (4 - 4) - 8 == 13 - 8

(10, 2, 15, 9)
2-10 + 15-2 + 9-15 == 9 + (15 - 15) + (2 - 2) - 10 == 9 - 10

However
(10, 2, 15, 9)
abs(2-10) + abs(15-2) + abs(9-15) != 9 - 10 or abs(9-10)
ndc85430 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  search in dict inside tuple steg 1 682 Mar-29-2023, 01:15 PM
Last Post: rob101
  Sort Differences in 2.7 and 3.10 Explained dgrunwal 2 1,355 Apr-27-2022, 02:50 AM
Last Post: deanhystad
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,795 Nov-04-2020, 11:26 AM
Last Post: Aggam
  How to compare two PDFs for differences Normanie 2 2,387 Jul-30-2020, 07:31 AM
Last Post: millpond
  Being explicit about the items inside a tuple argument rudihammad 3 2,443 Dec-04-2019, 08:10 AM
Last Post: perfringo
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,185 May-21-2019, 11:39 AM
Last Post: avorane
  Condition check differences and how to organise code? adam2020 4 2,668 May-12-2019, 04:12 PM
Last Post: Yoriz
  Iterate a tuple with dict inside anna 2 2,409 Feb-13-2019, 03:44 PM
Last Post: anna

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020