adding vectors

A

Andy Leszczynski

Hi,

Short question: why (1,"abc",0.3)+(2,"def",10.2) != (3,"abcdef",10.5)?

How to elegantly achieve (3,"abcdef",10.5) as a result of addition ...


Andy
 
P

Paul Rubin

Andy Leszczynski said:
Short question: why (1,"abc",0.3)+(2,"def",10.2) != (3,"abcdef",10.5)?
How to elegantly achieve (3,"abcdef",10.5) as a result of addition ...

tuple([(a+b) for a,b in zip((1,"abc",0.3),(2,"def",10.2))])
 
M

Mike Meyer

Paul Rubin said:
Andy Leszczynski said:
Short question: why (1,"abc",0.3)+(2,"def",10.2) != (3,"abcdef",10.5)?
How to elegantly achieve (3,"abcdef",10.5) as a result of addition ...
tuple([(a+b) for a,b in zip((1,"abc",0.3),(2,"def",10.2))])
[3, 'abcdef', 10.5]

Not having to do the zip is win. operator.add is a lose. I'm not sure
either is what I'd call elegant.

As for the "why" question, it's because the current behavior is more
generally useful. You can always concatenate two lists to get a longer
lists. The elements in a list don't have to support add, so using "+"
to denote elementwise addition is sorta pointless.

<mike
 
A

Alex Martelli

Andy Leszczynski said:
Hi,

Short question: why (1,"abc",0.3)+(2,"def",10.2) != (3,"abcdef",10.5)?

Because '+' applied to sequences means to concatenate them -- a more
frequent need than "element by element addition" (which I notice you
would NOT want to apply to strings, only to sequences for which it
happens to be convenient for your specific app...!-).
How to elegantly achieve (3,"abcdef",10.5) as a result of addition ...

If you mean "by using operator + on tuples", no way. If you're not hung
up on syntax, e.g.

def elemadd(t1, t2):
return tuple(i1+i2 for i1, i2 in zip(t1, t2))

or any of several other ways.


Alex
 
P

Paul Rubin

Mike Meyer said:
[3, 'abcdef', 10.5]

Not having to do the zip is win. operator.add is a lose. I'm not sure
either is what I'd call elegant.

Yeah, I didn't bother checking whether you could pass dyadic functions
to map. Given that you can, I see nothing wrong with lambda x,y: x+y
instead of operator.add, but that's just me.
 
T

Terry Hancock

Short question: why (1,"abc",0.3)+(2,"def",10.2) !=
(3,"abcdef",10.5)?

How to elegantly achieve (3,"abcdef",10.5) as a result of
addition ...

(a,b,c) is a "tuple", not a "vector".

IMHO, the "elegant" thing to do is to define a vector class
and use it. For convenience, allow a tuple initializer:

V = Vector
a = V(1,"abc",0.3) + V(2,"def",10.2)

Of course, the class "Vector" will have to define math
operators appropriately. Note that "%" has the correct
precedence to sub for cross-product, and sort of looks like
an X if you squint hard enough ;-).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,366
Members
48,057
Latest member
AntoineGre

Latest Threads

Top