473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Iteration over two sequences

I am just starting to learn Python, mostly by going through the examples
in Dive Into Python and by playing around.

Quite frequently, I find the need to iterate over two sequences at the
same time, and I have a bit of a hard time finding a way to do this in a
"pythonic" fashion. One example is a dot product. The straight-ahead
C-like way of doing it would be:

def dotproduct(a, b):
psum = 0
for i in range(len(a)):
psum += a[i]*b[i]
return psum

However, the range(len(a)) term is awfully un-pythonic :)
The built-in function map() gives me a way of "transposing" the a list
and the b list, and now I can handle it with a list comprehension:

def dotproduct(a, b):
return sum([x*y for x, y in map(None, a, b)])

My concern is one of efficiency: it seems to me that I have two loops
there: first one implied with map(...) and then the for loop -- which
seems like a waste since I feel I should be able to do the
multiplication via an argument to map. So far I have come up with an
alternative via defining a separate function:

def dotproduct(a, b):
def prod(x,y): return x*y
return sum(map(prod, a, b))

I suppose I could also use a lambda here -- but is there a different,
efficient, and obvious solution that I'm overlooking?
Thanks,
Henrik

--
"On some great and glorious day the plain folks of the land will reach
in their heart's desire at last and the White House will be adorned by
a downright moron."
-H.L. Mencken (1880-1956) American Writer
Jul 18 '05 #1
14 1630
zip or izip is your friend:

import itertools

a = [1,2,3]
b = ['a', 'b', 'c']

for a,b in itertools.izip(a, b):
print a, b

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
"Henrik Holm" <ne*******@henrikholm.com> wrote in message
news:1gq9qs9.3snutr1s4mcn2N%ne*******@henrikholm.c om...
I am just starting to learn Python, mostly by going through the examples
in Dive Into Python and by playing around.

Quite frequently, I find the need to iterate over two sequences at the
same time, and I have a bit of a hard time finding a way to do this in a
"pythonic" fashion. One example is a dot product. The straight-ahead
C-like way of doing it would be:

def dotproduct(a, b):
psum = 0
for i in range(len(a)):
psum += a[i]*b[i]
return psum

However, the range(len(a)) term is awfully un-pythonic :)
The built-in function map() gives me a way of "transposing" the a list
and the b list, and now I can handle it with a list comprehension:

def dotproduct(a, b):
return sum([x*y for x, y in map(None, a, b)])

My concern is one of efficiency: it seems to me that I have two loops
there: first one implied with map(...) and then the for loop -- which
seems like a waste since I feel I should be able to do the
multiplication via an argument to map. So far I have come up with an
alternative via defining a separate function:

def dotproduct(a, b):
def prod(x,y): return x*y
return sum(map(prod, a, b))

I suppose I could also use a lambda here -- but is there a different,
efficient, and obvious solution that I'm overlooking?
Thanks,
Henrik

--
"On some great and glorious day the plain folks of the land will reach
in their heart's desire at last and the White House will be adorned by
a downright moron."
-H.L. Mencken (1880-1956) American Writer


zip maybe?

def dotproduct(a,b):
return sum([x*y for x,y in zip(a,b)])

-- Paul
Jul 18 '05 #3

"Henrik Holm" <ne*******@henrikholm.com> wrote in message
news:1gq9qs9.3snutr1s4mcn2N%ne*******@henrikholm.c om...
I suppose I could also use a lambda here -- but is there a different,
efficient, and obvious solution that I'm overlooking?


Check the itertools recipes in the library documentation.
Jul 18 '05 #4
Richard Brodie <R.******@rl.ac.uk> wrote:
"Henrik Holm" <ne*******@henrikholm.com> wrote in message
news:1gq9qs9.3snutr1s4mcn2N%ne*******@henrikholm.c om...
I suppose I could also use a lambda here -- but is there a different,
efficient, and obvious solution that I'm overlooking?


Check the itertools recipes in the library documentation.


Thanks,

the itertools seem to contain several useful functions.

--
"On some great and glorious day the plain folks of the land will reach
in their heart's desire at last and the White House will be adorned by
a downright moron."
-H.L. Mencken (1880-1956) American Writer
Jul 18 '05 #5
> Quite frequently, I find the need to iterate over two sequences at
the
same time, and I have a bit of a hard time finding a way to do this in a "pythonic" fashion. One example is a dot product. The straight-ahead
C-like way of doing it would be:

def dotproduct(a, b):
psum = 0
for i in range(len(a)):
psum += a[i]*b[i]
return psum


for this particular example, the most pythonic way is to do nothing at
all, or, if you must call it dotproduct,
from Numeric import dot as dotproduct


Jul 18 '05 #6
I tried this and I got:

[(1, 'a'), (2, 'b'), (3, 'c')]

But if I change:

a=[1,2]

I got:

[(1, 'c')]

Why is that? I thought I should be getting:

[(1, 'a'),(2,'b')]

?????
"Diez B. Roggisch" <de*********@web.de> wrote in message
news:34*************@individual.net...
zip or izip is your friend:

import itertools

a = [1,2,3]
b = ['a', 'b', 'c']

for a,b in itertools.izip(a, b):
print a, b

--
Regards,

Diez B. Roggisch

Jul 18 '05 #7
John Lenton <jl*****@gmail.com> wrote:
def dotproduct(a, b):
psum = 0
for i in range(len(a)):
psum += a[i]*b[i]
return psum


for this particular example, the most pythonic way is to do nothing at
all, or, if you must call it dotproduct,
from Numeric import dot as dotproduct


Downloading, installing, and getting to know numerical modules for
Python is mext on my list :). However, I was under the impression that
Numarray is preferred to Numeric -- is that correct? Are these two
competing packages? (Hopefully this is not flame war bait...)

--
"On some great and glorious day the plain folks of the land will reach
in their heart's desire at last and the White House will be adorned by
a downright moron."
-H.L. Mencken (1880-1956) American Writer
Jul 18 '05 #8
Henrik Holm wrote:
John Lenton <jl*****@gmail.com> wrote:

def dotproduct(a, b):
psum = 0
for i in range(len(a)):
psum += a[i]*b[i]
return psum


for this particular example, the most pythonic way is to do nothing at
all, or, if you must call it dotproduct,
>from Numeric import dot as dotproduct

Downloading, installing, and getting to know numerical modules for
Python is mext on my list :). However, I was under the impression that
Numarray is preferred to Numeric -- is that correct? Are these two
competing packages? (Hopefully this is not flame war bait...)


Numarray is the replacement for Numeric. Some people are still using
Numeric for a variety of reasons, e.g. they use a package that has not
been updated to use numarray, they can't afford the performance penalty
that numarray has for small arrays, etc.

Steve
Jul 18 '05 #9
> Downloading, installing, and getting to know numerical modules for
Python is mext on my list :). However, I was under the impression that Numarray is preferred to Numeric -- is that correct? Are these two
competing packages? (Hopefully this is not flame war bait...)


Numeric's dot uses, if the appropriate libraries are installed,
processor-tuned code (it tries to load blas, and an attempt to load
blas will load atlas if present). Last time I checked numarray did not,
and so Numeric's dot is (or was) faster than numarray's dot by an order
of magnitude (and more, as the size of the array grew).

Jul 18 '05 #10
Henrik Holm wrote:
John Lenton <jl*****@gmail.com> wrote:

def dotproduct(a, b):
psum = 0
for i in range(len(a)):
psum += a[i]*b[i]
return psum


for this particular example, the most pythonic way is to do nothing at
all, or, if you must call it dotproduct,
>from Numeric import dot as dotproduct

Downloading, installing, and getting to know numerical modules for
Python is mext on my list :). However, I was under the impression that
Numarray is preferred to Numeric -- is that correct? Are these two
competing packages? (Hopefully this is not flame war bait...)

Numarray is the future, Numeric is the "past", but in the present
Numeric is better (and more mature) at some stuff than Numarray.

Learning both is not much harder than learning one, actually (they
share a lot).

--Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #11

"It's me" <it***@yahoo.com> wrote in message
news:Mr*****************@newssvr21.news.prodigy.co m...
I tried this and I got:
[(1, 'a'), (2, 'b'), (3, 'c')]
But if I change:
a=[1,2]
I got:
[(1, 'c')]
Why is that? I thought I should be getting:
[(1, 'a'),(2,'b')]
?????


Cut and paste the actual input and output for an interactive session with a
freshly started interpreter and perhaps we can answer for sure.

Terry J. Reedy


Jul 18 '05 #12
My example is somewhat flawed because it assigns a and b the values of the
iteration - so in the end, b is 'c', and only setting a to [1,2] will show
your results.

Use c and d for the variables in the for-statments, and things work as
expected.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #13

"Scott David Daniels" <Sc***********@Acm.Org> wrote in message
news:41********@nntp0.pdx.net:
Numarray is the future, Numeric is the "past",


This statement is not obviously true.
See the recent discussion on the developer lists.
(Search for Numeric3.)
Alan Isaac
Jul 18 '05 #14
David Isaac wrote:
"Scott David Daniels" <Sc***********@Acm.Org> wrote in message
news:41********@nntp0.pdx.net:
Numarray is the future, Numeric is the "past",


This statement is not obviously true. See the recent discussion on the
developer lists. (Search for Numeric3.)
Alan Isaac


I stand corrected. Sorry to have conveyed a mis-impression that I have
held for a while. It was my understanding that the Numeric code was so
hard to maintain that our fearless leader would never let it in the
codebase. Hence, Numarray was born. A quick look through some of the
Numeric3 messages leaves me with the impression that Numeric3 will be
more of a rebirth than a small change. I have used Numeric for several
big projects (that died for commercial, rather than technical, reasons),
and have certainly appreciated it. The thrust of my comment holds,
however, "learning both is not much harder than learning one."

--Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #15

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

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
59
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The...
31
by: Raymond Hettinger | last post by:
Based on your extensive feedback, PEP 322 has been completely revised. The response was strongly positive, but almost everyone preferred having a function instead of multiple object methods. The...
10
by: DeepBleu | last post by:
I noticed the following: >>a = 'abba' >>for n in a: >> print n, a.index(n) a 0 b 1 b 1 a 0 (expected result:
6
by: tkpmep | last post by:
I have list of lists of the following form L=, , , ] I want to aggregate these lists, i.e. to reduce L to L=, ] #500 = 100+400, 200=300-100 Here's how I have done it: L.sort() for i in...
4
by: temp | last post by:
Hi All, I wonder could someone help me with this? What I want to do is search through a list of letters and look for adjacent groups of letters that form sequences, not in the usual way of...
10
by: Vilson farias | last post by:
Greetings, I'm getting a big performance problem and I would like to ask you what would be the reason, but first I need to explain how it happens. Let's suppose I can't use sequences (it seams...
28
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are...
4
by: JJ | last post by:
Is there a way of checking that a line with escape sequences in it, has no strings in it (apart from the escape sequences)? i.e. a line with \n\t\t\t\t\t\t\t\r\n would have no string in it a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...

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.