473,803 Members | 4,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

consistency: extending arrays vs. multiplication ?

Hi all,

Just having started with python, I feel that simple array operations '*'
and '+' don't do multiplication/addition but instead extend/join an
array:

a=[1,2,3]
b=[4,5,6]
a+b [1, 2, 3, 4, 5, 6]

instead of what I would have expected:
[5,7,9]

or
2*a

[1, 2, 3, 1, 2, 3]

Well it is consistent to strings but tolerating string-operations to be
special is ok to me as "a" + "b" -> 'ab' :)
Why not make it another function like a.stretch() or a.expand() and
a.extend() is there doing the same anyway and is more readable...

Putting this in a larger view:
Ufuncs are very reasonable sin(a), etc ... all that won't work because
of that '+','*' syntax. Ok I can use numarray for that, but seeing its
PEP and a possible inclusion into python at some point that
inconsistency is giving me quite some headache...

Will that change in the future ? Or is this 100*[0] syntax put into
stone for all ages ?

Best,
Soeren.

PS: As I am very new to python please forgive/correct me!

Jul 23 '05 #1
11 2289
In <ma************ *************** ************@py thon.org>, Soeren
Sonnenburg wrote:
Just having started with python, I feel that simple array operations '*'
and '+' don't do multiplication/addition but instead extend/join an
array:

a=[1,2,3]
b=[4,5,6]
a+b

[1, 2, 3, 4, 5, 6]


Both operate on the lists themselves and not on their contents. Quite
consistent if you ask me.

Ciao,
Marc 'BlackJack' Rintsch
Jul 23 '05 #2
On Sat, 23 Jul 2005 18:30:02 +0200, Soeren Sonnenburg wrote:
Hi all,

Just having started with python, I feel that simple array operations '*'
and '+' don't do multiplication/addition but instead extend/join an
array:


* and + are not array operations, they are list operations.

Lists in Python can contain anything, not just numeric values.

Python doesn't have built-in mathematical arrays, otherwise known as
matrices. There are modules that do that, but I haven't used them. Google
on Numeric Python.

--
Steven.

Jul 24 '05 #3
Soeren Sonnenburg wrote:
Hi all,

Just having started with python, I feel that simple array operations '*'
and '+' don't do multiplication/addition but instead extend/join an
array:

a=[1,2,3]
b=[4,5,6]
a+b

[1, 2, 3, 4, 5, 6]

instead of what I would have expected:
[5,7,9]


To get what you expected, use

[x + y for (x, y) in zip(a, b)]

Jul 24 '05 #4
On Sat, 2005-07-23 at 23:35 +0200, Marc 'BlackJack' Rintsch wrote:
In <ma************ *************** ************@py thon.org>, Soeren
Sonnenburg wrote:
Just having started with python, I feel that simple array operations '*'
and '+' don't do multiplication/addition but instead extend/join an
array:

a=[1,2,3]
> b=[4,5,6]
> a+b

[1, 2, 3, 4, 5, 6]


Both operate on the lists themselves and not on their contents. Quite
consistent if you ask me.


But why ?? Why not have them operate on content, like is done on
*arrays ?

Soeren

Jul 24 '05 #5
On Sun, 2005-07-24 at 13:36 +1000, Steven D'Aprano wrote:
On Sat, 23 Jul 2005 18:30:02 +0200, Soeren Sonnenburg wrote:
Hi all,

Just having started with python, I feel that simple array operations '*'
and '+' don't do multiplication/addition but instead extend/join an
array:
* and + are not array operations, they are list operations.

Lists in Python can contain anything, not just numeric values.


That seems to be *the point*. Although list(a) + list(b) could create a
list [ a[0]+b[0], ...] and bail out if for elements '+' is not
defined...
Python doesn't have built-in mathematical arrays, otherwise known as
matrices. There are modules that do that, but I haven't used them. Google
on Numeric Python.


Well I am aware of that but I don't understand the reasons of having
both lists (which are infect arrays) and *arrays ? *I* would rather drop
'+' and '*' to work like they do in *array ...

Soeren

Jul 24 '05 #6
On Sat, 2005-07-23 at 20:25 -0700, Dan Bishop wrote:
Soeren Sonnenburg wrote:
Hi all,

Just having started with python, I feel that simple array operations '*'
and '+' don't do multiplication/addition but instead extend/join an
array:

a=[1,2,3]
>> b=[4,5,6]
>> a+b

[1, 2, 3, 4, 5, 6]

instead of what I would have expected:
[5,7,9]


To get what you expected, use

[x + y for (x, y) in zip(a, b)]


Thanks for this suggestion, however I am interested in understanding the
design decision here... I could aswell just use numarray and get the
wanted a+b by:

from numarray import *
a=array([1,2,3])
b=array([1,2,3])
a+b
array([2, 4, 6])

Soeren

Jul 24 '05 #7
Soeren Sonnenburg wrote:
On Sun, 2005-07-24 at 13:36 +1000, Steven D'Aprano wrote:
On Sat, 23 Jul 2005 18:30:02 +0200, Soeren Sonnenburg wrote:
Hi all,

Just having started with python, I feel that simple array operations '*'
and '+' don't do multiplication/addition but instead extend/join an
array:
* and + are not array operations, they are list operations.

Lists in Python can contain anything, not just numeric values.


That seems to be *the point*.


Whose point? If you mean that you want to be able to use arbitrary
objects in an array, then look in numarray.object s for an array type
that handles arbitrary Python objects.
Although list(a) + list(b) could create a
list [ a[0]+b[0], ...] and bail out if for elements '+' is not
defined...
Unlike the current situation, where a+b always works consistently
despite the contents, despite how long the lists are.
Python doesn't have built-in mathematical arrays, otherwise known as
matrices. There are modules that do that, but I haven't used them. Google
on Numeric Python.


Well I am aware of that but I don't understand the reasons of having
both lists (which are infect arrays)


They "are in [fact] arrays" only in the sense that they are containers
of objects with a contiguous layout in memory. That doesn't imply either
set of semantics for + and * operators.
and *arrays ?
They're good at different things. Arrays like Numeric/numarray are
harder to implement than the builtin lists.
*I* would rather drop
'+' and '*' to work like they do in *array ...


Tough. It's 14 years or so too late to make that change.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 24 '05 #8


Soeren Sonnenburg wrote:
On Sun, 2005-07-24 at 13:36 +1000, Steven D'Aprano wrote:
On Sat, 23 Jul 2005 18:30:02 +0200, Soeren Sonnenburg wrote:
Hi all,

Just having started with python, I feel that simple array operations '*'
and '+' don't do multiplication/addition but instead extend/join an
array:


* and + are not array operations, they are list operations.

Lists in Python can contain anything, not just numeric values.


That seems to be *the point*. Although list(a) + list(b) could create a
list [ a[0]+b[0], ...] and bail out if for elements '+' is not
defined...
Python doesn't have built-in mathematical arrays, otherwise known as
matrices. There are modules that do that, but I haven't used them. Google
on Numeric Python.


Well I am aware of that but I don't understand the reasons of having
both lists (which are infect arrays) and *arrays ? *I* would rather drop
'+' and '*' to work like they do in *array ...

The number of programmers who do operations on mathematical arrays is
pretty small. The number of programmers who need to do things like
concatenate lists is much larger. Thus, the decision was made to use
the valuable operator for the more common thing.

Truth be told, I rarely use + on lists (I tend to use list.extend
mostly), and if + had instead been used for element-by-element
operations, I don't think it would have affected the overall quality of
Python too much. But, as it's been said, it's a little late to change
it now.
--
CARL BANKS

Jul 24 '05 #9
Soeren Sonnenburg wrote:
On Sat, 2005-07-23 at 23:35 +0200, Marc 'BlackJack' Rintsch wrote:
Both operate on the lists themselves and not on their contents. Quite
consistent if you ask me.
But why ?? Why not have them operate on content, like is done on
*arrays ?


Because they're lists, not arrays. What do you propose that the
following do:

[1,2,3] + [4,5,6]
[1,2] + [3,4,5]
[1,2] + [{3:4,5:6}]
dict_var_1.keys () + dict_var_2.keys ()
[g(3) for g in [f1 f2 f3] + [f4 f5 f6]]

I point out that the idiom is <list> + <list>, not <numbers> +
<numbers>. Operations on lists must deal with them as lists, not lists
of any specific type.
Jul 24 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1942
by: Matthew Barnes | last post by:
I was wondering if there would be any interest in extending the struct.unpack format notation to be able to express groups of data with parenthesis. For example: >>> data = struct.pack('iiii', 1, 2, 3, 4) >>> struct.unpack('i(ii)i', data) # Note the parentheses (1, (2, 3), 4)
9
8003
by: Ralf Hildebrandt | last post by:
Hi all! First of all: I am a C-newbie. I have noticed a "strange" behavior with the standart integer multiplication. The code is: void main(void)
9
6678
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the function (which was translated from Fortran code), among other heinous and numerous declarations, is this bit: static float bbuff; static int bkey; static int buse;
5
2166
by: chella mani | last post by:
hi all, I am developing a tool for scientific application which must be time and memory efficient,my tool uses 1D,2D and 3D arrays. Is it a good practice to use 3D arrays in applications or I should use convert my 3D arrays to 1D arrays for efficiency. Thanks in advance, Chella mani
8
20528
by: l.j. | last post by:
Hi. I've a homework including calculations with arrays. I've stuck on multiply them. Can you help me?
1
2694
by: conor.robinson | last post by:
Using large arrays of data I found it is MUCH faster to cast arrays to matricies and then multiply the two matricies togther (scipy.matrix(ARRAY1)*scipy.matrix(ARRAY2)) in order to do a matrix multipy of two arrays vs. scipy.matrixmultipy(ARRAY1, ARRAY2). Are there any logical/efficiency errors with this train of thought and is there a valid reason for the speed increase? Thanks, Conor
5
2867
by: paul | last post by:
Hi all, Could some kind soul peruse the following code and see if there is anything wrong with it? Its producing output, but its only occupying the first third of the output array; to give an example, think of a TV screen where only the top third shows anything, and what is does show is repeated 3 times. The code should skip through an array of bytes, taking them in groups of three, and then writing the results of a calculation on the...
11
1529
by: Hakusa | last post by:
Pythonic lists are everything I want in a one dimensional array . . . but I'm trying to do a text adventure and simplify the proces by creating a grid as opposed to a tedious list of rooms this room connects to. So I want to know a good way to do a SIMPLE two dimensional array. Python's lists are a little confusing when it comes to how to do this. I realized that I could do list = * N ] * N
6
1920
by: Emiurgo | last post by:
Hi there to everyone! I've got a problem which may be interesting to some of you, and I'd be very grateful if there is someone who can give me some advice (or maybe redirect me to some other place). Very brief introduction. Since I'm learning vectorization with SSE (I use gcc 4.1.3 on Ubuntu 7.10) I've built a simple test program in C for comparing running times. I wanted to see how much improvement could give vectorization to simple...
0
9562
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10309
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10289
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9119
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7600
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6840
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5496
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.