dot operations

Paddy paddy3118 at netscape.net
Thu Jan 11 08:37:38 EST 2007


jm.suresh at no.spam.gmail.com wrote:
> Hi,
>  Frequently I get to do like this:
> a = (1, 2, 3, 4) # some dummy values
> b = (4, 3, 2, 1)
> import operator
> c = map(operator.add, a, b)
>
> I am finding the last line not very readable especially when I combine
> couple of such operations into one line. Is it possible to overload
> operators, so that, I can use .+ for element wise addition, as,
> c = a .+ b
> which is much more readable.
>
> Similarly, I want to use .- , .*, ./   . Is it possible to do?
>
> thanks.
>
> -
> Suresh

List comprehensions?

>>> a = (1, 2, 3, 4)
>>> b = (4, 3, 2, 1)
>>> import operator
>>> c = map(operator.add, a, b)
>>> c
[5, 5, 5, 5]
>>> c1 = [a1+b1 for a1,b1 in zip(a,b)]
>>> c1
[5, 5, 5, 5]
>>> 


- Paddy.




More information about the Python-list mailing list