Parallel arithmetic?

  • Thread starter Terrance N. Phillip
  • Start date
T

Terrance N. Phillip

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
 
P

Paul Rubin

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

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

Michael Hoffman

Terrance said:
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)
 
L

Lonnie Princehouse

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) )
 
R

Robert Kern

Terrance said:
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.)

If you're doing this kind of thing often, look into using Numeric.

http://numeric.scipy.org

In [21]: from Numeric import array

In [22]: a = array(range(10))

In [23]: b = array(range(10, 20))

In [24]: c = a - b

In [25]: c
Out[25]: [-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,]

--
Robert Kern
(e-mail address removed)

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

jepler

If you use numarray, you *can* write
c = a-b
import numarray
a = numarray.array([1,2,3])
b = numarray.array([5,0,2])
c = a-b
c
array([-4, 2, 1])

numarray is packaged separately from Python.
http://www.stsci.edu/resources/software_hardware/numarray

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFC8qGZJd01MZaTXX0RAvAsAJ9WfsYQZOwDnLeHua0Wrw7kWjT/+QCgiYwT
HfGp0vKbezN0qwOzCNF7e5A=
=wFXg
-----END PGP SIGNATURE-----
 
T

Terry Reedy

"#"map" will be removed from the next versions of python.

The next version will be 2.5. Map will not go away then.
In 3.0, in the indefinite future, it might go away, it might just be moved.

tjr
 
T

Terrance N. Phillip

Thank-you very much for all the excellent replies. I'm thinking of using
this to determine if a sequence is a "run" (as in a card game). If I've
got a sorted hand [3, 4, 5, 6, 7], then I know I've got a 5-card run
because [4, 5, 6, 7] - [3, 4, 5, 6] == [1, 1, 1, 1]. I want to avoid
something like
if h[0] == h[1]-1 and h[1] == h[2]-1 ...

Nick.
 
R

Robert Kern

Terrance said:
Thank-you very much for all the excellent replies. I'm thinking of using
this to determine if a sequence is a "run" (as in a card game). If I've
got a sorted hand [3, 4, 5, 6, 7], then I know I've got a 5-card run
because [4, 5, 6, 7] - [3, 4, 5, 6] == [1, 1, 1, 1]. I want to avoid
something like
if h[0] == h[1]-1 and h[1] == h[2]-1 ...

In that case:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/415504

--
Robert Kern
(e-mail address removed)

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

Dennis Lee Bieber

Thank-you very much for all the excellent replies. I'm thinking of using
this to determine if a sequence is a "run" (as in a card game). If I've
got a sorted hand [3, 4, 5, 6, 7], then I know I've got a 5-card run

A sorted list?

if (hand[-1] - hand[0]) == (len(hand) - 1)

would seem to do it for your example.

Actually, if you KNOW the list is only 5 entries long

if (hand[4] - hand[0]) == 4

would do it.

Or any equivalent... (hand[0] + 4) == hand[4]


--
 
J

jburgy

Dennis said:
Thank-you very much for all the excellent replies. I'm thinking of using
this to determine if a sequence is a "run" (as in a card game). If I've
got a sorted hand [3, 4, 5, 6, 7], then I know I've got a 5-card run

A sorted list?

if (hand[-1] - hand[0]) == (len(hand) - 1)

would seem to do it for your example.

Actually, if you KNOW the list is only 5 entries long

if (hand[4] - hand[0]) == 4

It's cute but wrong! How 'bout hand = [ 0, 0, 0, 4 ]? It's sorted,
passes your test and does not meet the OP's requirement :(
would do it.

Or any equivalent... (hand[0] + 4) == hand[4]


--
============================================================== <
(e-mail address removed) | Wulfraed Dennis Lee Bieber KD6MOG <
(e-mail address removed) | Bestiaria Support Staff <
============================================================== <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,311
Members
47,982
Latest member
DrewDemoss

Latest Threads

Top