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 5 5056
Andy Leszczynski <ya***@nospam.leszczynscy> writes: 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))])
Paul Rubin <http://ph****@NOSPAM.invalid> writes: Andy Leszczynski <ya***@nospam.leszczynscy> writes: 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))]) map(operator.add, (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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Andy Leszczynski <ya***@nospam.leszczynscy> wrote: 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
Mike Meyer <mw*@mired.org> writes: map(operator.add, (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.
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.
On Mon, 19 Dec 2005 22:06:31 -0500
Andy Leszczynski <ya***@nospam.leszczynscy> wrote: 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 ;-).
--
Terry Hancock (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Noo |
last post by:
Hi. I've got some code that uses vectors fairly extensively and it needs to
be efficient. Therefore I'm using reserve() quite a bit. However what other
functions (are supposed to) change the...
|
by: Simon Elliott |
last post by:
I'd like to do something along these lines:
struct foo
{
int i1_;
int i2_;
};
struct bar
{
|
by: Dr. J.K. Becker |
last post by:
Hi all,
I have vectors that holds pointers to other vectors, like so:
vector<whatever> x;
vector<whatever*> z;
z=&x;
Now I add something to x
|
by: Adam Hartshorne |
last post by:
Hi All,
I have the following problem, and I would be extremely grateful if
somebody would be kind enough to suggest an efficient solution to it.
I create an instance of a Class A, and...
|
by: mahurshi |
last post by:
I've got a gate structure that looks like this
/* Defining sGATE structure */
struct sGATE
{
string name;
vector<int> input;
int output;
};
|
by: Chris Roth |
last post by:
vector<doublev1;
vector<doublev2;
What is the best way to add v1 to v2? (v2 = v1+v2 that is)? Simply
iterate through, or can an algorithm like transform be used? I have used
transform to add a...
|
by: japuentem |
last post by:
Hi
I have a main Vector and i'm adding another vectors inside the main, the question is ¿how to get each value of the secondary vectors?.
I hope you understand the question.
|
by: Rob |
last post by:
How would I do this?
I want to be able to handle vectors of many different types of data
and vectors that can contain any number of other vectors of data.
Currently, I have a templated...
|
by: Laura Wilkinson |
last post by:
I've got many differences between vectors/strings and arrays but I need more details. I guess I don't really understand it enough to do this but this is what I've concluded so far;
An array is a...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |