没有for循环的数组操作 [英] array manipulation without for loops

查看:40
本文介绍了没有for循环的数组操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有两个相同尺寸的阵列,但有3个不同的

值:255,1或2.

我想设置两个数组中255的所有位置是

相等,即,一个数组有255,我在
$中设置相同的元素b $ b其他数组为255,反之亦然。有没有人知道如何做这个

而不使用for循环?


此致,

谢尔顿

推荐答案

Sheldon写道:
Sheldon wrote:


我有两个相同维度的阵列但是有3个不同的值:255,1或2.
我想设置两个数组中255的所有位置相等,即一个数组有255,我设置
其他数组中的相同元素为255,反之亦然。有没有人知道怎么做这个
而不使用for循环?

真诚的,
谢尔顿
Hi,

I have two arrays that are of the same dimension but having 3 different
values: 255, 1 or 2.
I would like to set all the positions in both arrays having 255 to be
equal, i.e., where one array has 255, I set the same elements in the
other array to 255 and visa versa. Does anyone know how to do this
without using for loops?

Sincerely,
Sheldon



无论是什么?你有什么反对for循环吗?


然而......


你可以自己循环:

i = 0

而i<无论如何:

#...用我做点什么

i + = 1


但重点是什么?这与for循环相同但速度较慢。


如果你不想要任何类型的循环(再次,有什么意义?)你

可以写一些递归的东西:

def proc(i,...):

#...用i br做点什么/>
如果我<无论如何:

proc(i + 1,...)


但这会更慢。


Gary Herron


Whatever for? Have you got something against for loops?

However...

You could roll you own loop:
i=0
while i < whatever:
# ... do something with i
i += 1

But what''s the point? This does the same as a for loop but slower.

If you don''t want any kind of a loop (again, What''s the point?) you
could write something recursive:

def proc(i, ...):
# ... do something with i
if i < whatever:
proc(i+1, ...)

But this would be even slower.

Gary Herron


嗨Gary,


我真的想减少时间,因为我有600 +数组与

尺寸(1215,1215)进行比较,我用

数组做了很多事情。如果我理解正确,就无法绕过

循环?

/ Sheldon


Gary Herron写道:< br>
Hi Gary,

I am really trying to cut the time down as I have 600+ arrays with
dimensions (1215,1215) to compare and I do a lot more things with the
arrays. If I understand you correctly, there is no way around a for
loop?
/Sheldon

Gary Herron wrote:
Sheldon写道:
Sheldon wrote:


我有两个相同维度但有3个不同的阵列
值:255,1或2.
我想设置两个数组中255的所有位置相等,即,一个数组有255,我在其他数组为255,反之亦然。有没有人知道如何做这个
而不使用for循环?

真诚的,
谢尔顿
Hi,

I have two arrays that are of the same dimension but having 3 different
values: 255, 1 or 2.
I would like to set all the positions in both arrays having 255 to be
equal, i.e., where one array has 255, I set the same elements in the
other array to 255 and visa versa. Does anyone know how to do this
without using for loops?

Sincerely,
Sheldon


无论是为了什么?你有什么东西反对for循环吗?

然而......

你可以滚动你自己的循环:
i = 0
而我<无论如何:
#...用我做某事
我+ = 1

但重点是什么?这与for循环相同但速度较慢。

如果你不想要任何类型的循环(再次,有什么意义?)你可以写点什么递归:

def proc(i,...):
#...用i做某事
如果我<无论如何:
proc(i + 1,...)

但这会更慢。

Gary Herron


Whatever for? Have you got something against for loops?

However...

You could roll you own loop:
i=0
while i < whatever:
# ... do something with i
i += 1

But what''s the point? This does the same as a for loop but slower.

If you don''t want any kind of a loop (again, What''s the point?) you
could write something recursive:

def proc(i, ...):
# ... do something with i
if i < whatever:
proc(i+1, ...)

But this would be even slower.

Gary Herron






Sheldon< sh ****** @ gmail.com>写道:
Sheldon <sh******@gmail.com> wrote:
我有两个相同维度但有3个不同值的数组:255,1或2.
我想设置所有两个数组中255的位置相等,即一个数组有255的位置,我将
其他数组中的相同元素设置为255,反之亦然。有没有人知道如何做这个
而不使用for循环?
I have two arrays that are of the same dimension but having 3 different
values: 255, 1 or 2.
I would like to set all the positions in both arrays having 255 to be
equal, i.e., where one array has 255, I set the same elements in the
other array to 255 and visa versa. Does anyone know how to do this
without using for loops?




Python的数字扩展包(仍然可用,但不是主动的

开发了更多)和它的继承者(numarray,甚至更新的

numpy)充满了这样的功能 - 实际上,真正使用

阵列Python你应该得到这些软件包中的任何一个(Python只提供了标准库中非常有限的化身的
数组

模块名为array - 比什么都没有好,但很少内置

功能,而那些包有很多)。


当然,如果你说数组实际上你的意思是列表,

情况完全不同(可能值得将列表转换为

数值数组进行操作,然后当你进行操作时完成,如果速度是绝对的本质,你绝对需要列表作为输入和

输出,但中间有很多操作。

Alex



Python''s Numeric extension package (still available, but not actively
developed any more) and its successors (numarray, and the even newer
numpy) are replete with such functionality -- indeed, to really use
arrays in Python you should get any one of these packages (Python offers
arrays only in the very limited incarnation of the standard library
module named array -- better than nothing, but little built-in
functionality, while those packages have plenty).

Of course, if you say "array" when actually you mean "list", the
situation is completely different (it may be worth turning list into
Numeric arrays for manipulation, then back when you''re done, if speed is
absolutely of the essence and you desperately need lists as inputs and
outputs but a lot of manipulation in-between).
Alex


这篇关于没有for循环的数组操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆