一致性:扩展数组与乘法? [英] consistency: extending arrays vs. multiplication ?
问题描述
大家好,
刚刚开始使用python,我觉得简单的数组操作'''''
和''+''不进行乘法/加法,而是扩展/加入
数组:
a = [1,2,3]
b = [4,5,6]
a + b
[1,2,3,4,5,6]
而不是我的预期:
[5,7,9]
或
2 * a
[1,2,3,1,2,3]
嗯,它与字符串一致但是容忍字符串操作是
特殊对我来说是好的,因为a +b - > ''ab'':)
为什么不让它成为另一个函数,比如a.stretch()或a.expand()和
a.extend()在做什么无论如何都是相同的,并且更具可读性...
将它放在更大的视图中:
Ufuncs是非常合理的罪(a)等...所有这些都不会奏效,因为
的'+'','''''语法。好的我可以使用numarray,但是看到它的
PEP并且可能包含在python中某些时候
的不一致让我非常头疼...
将来会改变吗?或者这个100 * [0]语法是否适用于所有年龄段的
石头?
最佳,
Soeren。
PS:因为我对python很新,请原谅/纠正我!
在< ma *************************************** @ python。 org>,Soeren
Sonnenburg写道:
刚刚开始使用python,我觉得简单的数组操作'''''
和' '+''不做乘法/加法,而是扩展/加入
数组:
a = [1,2,3]< blockquote class =post_quotes> b = [4,5,6]
a + b
[1,2,3,4,5,6]
两者都在列表本身上运行,而不是在内容上运行。很好
如果你问我一致。
Ciao,
Marc''BlackJack''Rintsch
2005年7月23日星期六18:30:02 +0200,Soeren Sonnenburg写道:
大家好,
刚刚开始使用python,我觉得简单的数组操作'''''
和'''''不做乘法/加法,而是扩展/加入一个
数组:
*和+不是数组操作,它们是列表操作。
Python中的列表可以包含任何内容,而不仅仅是数值。
Python没有内置的数学数组,也称为
矩阵。有些模块可以做到这一点,但我还没有使用它们。 Google
on Numeric Python。
-
史蒂文。
<
Soeren Sonnenburg写道:大家好,
刚开始使用python,我觉得简单的数组操作'''''
并且''+''不做乘法/加法,而是扩展/加入
数组:
a = [1,2,3]b = [4,5,6]
a + b
[1,2,3,4,5,6]
而不是我的预期:
[5,7,9]
为了得到你的预期,请使用
[x + y代表(x,y)的拉链(a,b)]
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!
In <ma***************************************@python. 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
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.
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)]
这篇关于一致性:扩展数组与乘法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!