Adding Values From Tuples of Same Length

后端 未结 6 912
隐瞒了意图╮
隐瞒了意图╮ 2020-12-06 05:07

In a graphical program I\'m writing using pygame I use a tuple representing a coordinate like this: (50, 50).

Sometimes, I call a function which returns another tupl

相关标签:
6条回答
  • 2020-12-06 05:13

    This is a work in progress as I am learning Python myself. Can we use classes here, could simplify some operations later. I propose to use a coord class to store the coordinates. It would override add and sub so you could do addition and subtraction by simply using operators + and -. You could get the tuple representation with a function built into it.

    Class

    class coord(object):    
        def __init__(self,x,y):
            self.x = x
            self.y = y
    
        def __add__(self,c):
            return coord(self.x + c.x, self.y + c.y)
    
        def __sub__(self,c):
            return coord(self.x - c.x, self.y - c.y)
    
        def __eq__(self,c): #compares two coords
            return self.x == c.x and self.y == c.y
    
        def t(self): #return a tuple representation.
            return (self.x,self.y)
    

    Usage

    c1 = coord(4,3) #init coords
    c2 = coord(3,4)
    
    c3 = c1 + c2    #summing two coordinates. calls the overload __add__
    print c3.t()    #prints (7, 7)
    c3 = c3 - c1
    print c3.t()    #prints (3, 4)
    print c3 == c2  #prints True
    

    you could improve coord to extend other operators as well (less than, greater than ..).

    In this version after doing your calculations you can call the pygame methods expecting tuples by just saying coord.t(). There might be a better way than have a function to return the tuple form though.

    0 讨论(0)
  • 2020-12-06 05:22

    To get your "+" and "+=" behaviour you can define your own class and implement the __add__() method. The following is an incomplete sample:

    # T.py
    class T(object):
        def __init__(self, *args):
            self._t = args
        def __add__(self, other):
            return T(*([sum(x) for x in zip(self._t, other._t)]))
        def __str__(self):
            return str(self._t)
        def __repr__(self):
            return repr(self._t)
    
    >>> from T import T
    >>> a = T(50, 50)
    >>> b = T(3, -5)
    >>> a
    (50, 50)
    >>> b
    (3, -5)
    >>> a+b
    (53, 45)
    >>> a+=b
    >>> a
    (53, 45)
    >>> a = T(50, 50, 50)
    >>> b = T(10, -10, 10)
    >>> a+b
    (60, 40, 60)
    >>> a+b+b
    (70, 30, 70)
    

    EDIT: I've found a better way...

    Define class T as a subclass of tuple and override the __new__ and __add__ methods. This provides the same interface as class tuple (but with different behaviour for __add__), so instances of class T can be passed to anything that expects a tuple.

    class T(tuple):
        def __new__(cls, *args):
            return tuple.__new__(cls, args)
        def __add__(self, other):
            return T(*([sum(x) for x in zip(self, other)]))
        def __sub__(self, other):
            return self.__add__(-i for i in other)
    
    >>> a = T(50, 50)
    >>> b = T(3, -5)
    >>> a
    (50, 50)
    >>> b
    (3, -5)
    >>> a+b
    (53, 45)
    >>> a+=b
    >>> a
    (53, 45)
    >>> a = T(50, 50, 50)
    >>> b = T(10, -10, 10)
    >>> a+b
    (60, 40, 60)
    >>> a+b+b
    (70, 30, 70)
    >>> 
    >>> c = a + b
    >>> c[0]
    60
    >>> c[-1]
    60
    >>> for x in c:
    ...     print x
    ... 
    60
    40
    60
    
    0 讨论(0)
  • 2020-12-06 05:25

    List comprehension is probably more readable, but here's another way:

    >>> a = (1,2)
    >>> b = (3,4)
    >>> tuple(map(sum,zip(a,b)))
    (4,6)
    
    0 讨论(0)
  • 2020-12-06 05:30

    Well, one way would be

    coord = tuple(sum(x) for x in zip(coord, change))
    

    If you are doing a lot of math, you may want to investigate using NumPy, which has much more powerful array support and better performance.

    0 讨论(0)
  • 2020-12-06 05:31

    As John Y mentions, this is pretty easy using numpy.

    import numpy as np
    
    x1 = (0,3)
    x2 = (4,2)
    tuple(np.add(x1,x2))
    
    0 讨论(0)
  • 2020-12-06 05:34

    My two cents, hope this helps

    >>> coord = (50, 50)
    >>> change = (3, -5)
    >>> tuple(sum(item) for item in zip(coord, change))
    (53, 45)
    
    0 讨论(0)
提交回复
热议问题