1

I've searched and couldn't find the way I'm looking for to add the sum when dealing with tuples. Both a and b are tuples which are immutable and I'm trying to add their values so I can find the max. I know how to do it with dictionaries by calling the sum function and max() but not with tuples though so any help please. I know it's kind of tricky since its a tuple and tuples are immutable and can't be changed, but i'm not changing them, I just want to know the sum and add the largest one to another tuple.

a = [(1,2),(4,5),(1,0)]
b = [(3,2),(9,4),(2,2)]

Output:

a=[(3),(9),(1)]
b=[(5),(13),(4)]

Since (4,5) = 9 in tuple a and thats the max of a, move it to another tuple and a should now be this:

a = [(1,2),(1,0)]

The same goes for tuple b.

1
  • Can you clarify? What is the desired result? Commented Jan 15, 2013 at 2:56

3 Answers 3

5

Tuples' being immutable has nothing to do with this. You can still use max and sum here:

>>> max(a, key=sum)
(4, 5)

Then do whatever you want with it. A full incantation might look like:

maximums = []
for tuples in (a, b):
    max_tuple = max(tuples, key=sum)
    tuples.remove(max_tuple)
    maximums.append(tuples)
Sign up to request clarification or add additional context in comments.

5 Comments

when I try to do your above code it gives an error that says: unsupported operand type(s) for +: 'int' and 'str'. I think the error is coming from key=sum in the max paramter
max only accepts one parameter from what I'm reading here tutorialspoint.com/python/tuple_max.htm
You should use the real docs, not tutorials. max has had a key argument since 2.5, which is forever ago.
The error you got means that one of your tuples has both ints and strs in it. You can't reasonably get a maximum sum out of that. Fix them to only contain ints.
ooohh i see what I was doing wrong thank you very much for clearly that up. Thank you so much god bless you and your family for the help much appreciated
3

if you want to remove max tuple from each list then:

In [17]: a=[(1,2),(4,5),(1,0)]

In [18]: a.remove(max(a, key=sum))

In [19]: a
Out[19]: [(1, 2), (1, 0)]

In [20]: b = [(3,2),(9,4),(2,2)]

In [21]: b.remove(max(b,key=sum))

In [22]: b
Out[22]: [(3, 2), (2, 2)]

or if you want to remove max tuple from one and add that to next one then::

In [34]: a=[(1,2),(4,5),(1,0)]

In [35]: b = [(3,2),(9,4),(2,2)]

In [36]: b.append(a.pop(a.index(max(a,key=sum))))

In [37]: b
Out[37]: [(3, 2), (9, 4), (2, 2), (4, 5)]

In [38]: a
Out[38]: [(1, 2), (1, 0)]

or if you want to remove maxx tuple from each list; and then add them to another list::

In [44]: maxx=[]

In [45]: a=[(1,2),(4,5),(1,0)]

In [46]: b = [(3,2),(9,4),(2,2)]

In [47]: maxx.append(a.pop(a.index(max(a,key=sum))))

In [48]: maxx.append(b.pop(b.index(max(b,key=sum))))

In [49]: a
Out[49]: [(1, 2), (1, 0)]

In [50]: b
Out[50]: [(3, 2), (2, 2)]

In [51]: maxx
Out[51]: [(4, 5), (9, 4)]

2 Comments

I thought you couldn't remove from a tuple since its immutable thats why I figured to add it to another tuple
yes the last example is what I'm trying to do. Thanks alot for taking the time to up me god bless you as well. People on this site are really helpful and generous thanks
0

So like:

a.remove(max(a,key=lambda x:x[0]+x[1]))
b.remove(max(b,key=lambda x:x[0]+x[1]))

?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.