并行算术? [英] Parallel arithmetic?

查看:78
本文介绍了并行算术?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定a和b,两个相等长度的整数列表,我希望c为

[a1-b1,a2-b2,...,an-bn]。我可以这样做:


c = [0] * len(a)

为ndx,项目为枚举(a):

c [ndx] = item - b [ndx]


但我想知道是否有更好的方法,或许可以避免循环?


尼克。


(我似乎记得从我遥远的过去,这种事情已经死了

轻松使用APL .. .c = ab,或多或少。)


N

解决方案

" Terrance N. Phillip" ; <我************* @ hotmail.com>写道:

给定a和b,两个相等长度的整数列表,我希望c为
[a1-b1,a2-b2,...,an-bn]。




c = [a [i] - b [i] for x in xrange(len(a))]


< blockquote> Terrance N. Phillip写道:

给定a和b,两个相等长度的整数列表,我希望c为
[a1-b1,a2-b2,...,一个-BN。我可以这样做:

c = [0] * len(a)
对于ndx,枚举项目(a):
c [ndx] = item - b [ndx]

但我想知道是否有更好的方法,或许可以避免循环?




这里''一种方式:


c = [a_item - b_item为a_item,b_item为zip(a,b)]


和另一个:


导入运算符

c = map(operator.sub,a,b)

-

迈克尔霍夫曼


有很多方法可以做到这一点。它们都没有避免循环,技术上是
,尽管你可以很容易地避免使用for。语法。


- 简单但浪费一些记忆

c = [ij for i,j in zip(a,b)]


- 使用itertools.izip(python 2.3)

c = [ij for i,j in itertools.izip(a,b)]


- 生成器表达式(python 2.4)

c =(ij for i,j in itertools.izip(a,b))


Given a and b, two equal length lists of integers, I want c to be
[a1-b1, a2-b2, ... , an-bn]. I can do something like:

c = [0] * len(a)
for ndx, item in enumerate(a):
c[ndx] = item - b[ndx]

But I''m wondering if there''s a better way, perhaps that avoids a loop?

Nick.

(I seem to recall from my distant past that this sort of thing was dead
easy with APL... c = a-b, more or less.)

N

解决方案

"Terrance N. Phillip" <me*************@hotmail.com> writes:

Given a and b, two equal length lists of integers, I want c to be
[a1-b1, a2-b2, ... , an-bn].



c = [a[i] - b[i] for i in xrange(len(a))]


Terrance N. Phillip wrote:

Given a and b, two equal length lists of integers, I want c to be
[a1-b1, a2-b2, ... , an-bn]. I can do something like:

c = [0] * len(a)
for ndx, item in enumerate(a):
c[ndx] = item - b[ndx]

But I''m wondering if there''s a better way, perhaps that avoids a loop?



Here''s one way:

c = [a_item - b_item for a_item, b_item in zip(a, b)]

And another:

import operator
c = map(operator.sub, a, b)
--
Michael Hoffman


There are many ways to do this. None of them avoids looping,
technically, although you can easily avoid the "for" syntax.

-- Simple but wastes some memory
c = [i-j for i,j in zip(a,b)]

-- Using itertools.izip (python 2.3)
c = [i-j for i,j in itertools.izip(a,b) ]

-- Generator expression (python 2.4)
c = ( i-j for i,j in itertools.izip(a,b) )


这篇关于并行算术?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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