473,394 Members | 2,020 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,394 software developers and data experts.

for x,y in word1, word2 ?

Is there a syntax for looping through 2 iterables at the same time?

for x in y:
for a in b:
is not what I want.

I want:
for x in y and for a in b:
Aug 11 '08 #1
7 1227
On Aug 10, 11:18�pm, ssecorp <circularf...@gmail.comwrote:
Is there a syntax for looping through 2 iterables at the same time?

for x in y:
� � for a in b:

is not what I want.

I want:
for x in y and for a in b:
Something like this?
>>a = ['a','b','c']
b = [1,2,3]
zip(a,b)
[('a', 1), ('b', 2), ('c', 3)]

Aug 11 '08 #2
On Aug 11, 6:40*am, Mensanator <mensana...@aol.comwrote:
On Aug 10, 11:18 pm, ssecorp <circularf...@gmail.comwrote:
Is there a syntax for looping through 2 iterables at the same time?
for x in y:
for a in b:
is not what I want.
I want:
for x in y and for a in b:

Something like this?
>a = ['a','b','c']
b = [1,2,3]
zip(a,b)

[('a', 1), ('b', 2), ('c', 3)]
I know zip but lets say I have a word "painter" and I want to compare
it to a customer's spelling, he might have written "paintor" and I
want to check how many letters are the same.

Now I know how I could do this, it is not hard.
I am just wondering if these is any specific simple syntax for it.
Aug 11 '08 #3
On Aug 11, 5:40 am, Mensanator <mensana...@aol.comwrote:
On Aug 10, 11:18 pm, ssecorp <circularf...@gmail.comwrote:
Is there a syntax for looping through 2 iterables at the same time?
for x in y:
for a in b:
is not what I want.
I want:
for x in y and for a in b:

Something like this?
>a = ['a','b','c']
b = [1,2,3]
zip(a,b)

[('a', 1), ('b', 2), ('c', 3)]
I would have thought the difflib library and SequenceMatcher would do
(more than) what you're after.

Just an idea anyway,

Jon.

Aug 11 '08 #4
On Aug 10, 11:14*pm, ssecorp <circularf...@gmail.comwrote:
On Aug 11, 6:40*am, Mensanator <mensana...@aol.comwrote:
On Aug 10, 11:18 pm, ssecorp <circularf...@gmail.comwrote:
Is there a syntax for looping through 2 iterables at the same time?
for x in y:
for a in b:
is not what I want.
I want:
for x in y and for a in b:
Something like this?
>>a = ['a','b','c']
>>b = [1,2,3]
>>zip(a,b)
[('a', 1), ('b', 2), ('c', 3)]

I know zip but lets say I have a word "painter" and I want to compare
it to a customer's spelling, he might have written "paintor" and I
want to check how many letters are the same.

Now I know how I could do this, it is not hard.
I am just wondering if these is any specific simple syntax for it.
There are two answers: first, if your domain interest is spell-
checking, search for "phonetic algorithm" or "soundex python". If your
interest is iteration, check out itertools - AFAIK this is the closest
you will get to a simple syntax for iterating over the diagonal as
opposed to the Cartesian product.
>>from itertools import *
for x,y in izip('painter','paintor'):
... print x == y
...

David
Aug 11 '08 #5
On Sun, 10 Aug 2008 23:14:50 -0700, ssecorp wrote:
I know zip but lets say I have a word "painter" and I want to compare it
to a customer's spelling, he might have written "paintor" and I want to
check how many letters are the same.

Now I know how I could do this, it is not hard. I am just wondering if
these is any specific simple syntax for it.
No special syntax for that, but you can combine the `sum()` function, a
generator expression and `zip()`:

In [40]: sum(int(a == b) for a, b in zip('painter', 'paintor'))
Out[40]: 6

Or this way if you think it's more clear:

In [41]: sum(1 for a, b in zip('painter', 'paintor') if a == b)
Out[41]: 6

Ciao,
Marc 'BlackJack' Rintsch
Aug 11 '08 #6
On Sun, 10 Aug 2008 21:40:55 -0700, Mensanator wrote:
On Aug 10, 11:18�pm, ssecorp <circularf...@gmail.comwrote:
>Is there a syntax for looping through 2 iterables at the same time?

for x in y:
� � for a in b:

is not what I want.

I want:
for x in y and for a in b:

Something like this?
>>>a = ['a','b','c']
b = [1,2,3]
zip(a,b)
[('a', 1), ('b', 2), ('c', 3)]
zip and the nested loops don't do the same thing
zipping then comparing would compare items that are at the same position
the nested loops would compare each item to all the items in the other
sequence, big difference
AFAIK there's no better way to accomplish what the nested loops do with
a simpler syntax
Aug 12 '08 #7
ssecorp <ci**********@gmail.comwrites:
for x in y and for a in b:
for x,a in zip(y,b): ...

or the iterator version:

from itertools import izip
for x,a in izip(y,b): ...

avoids allocating a list to iterate through.
Aug 12 '08 #8

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

Similar topics

3
by: PM | last post by:
I'm trying to make a kind of search history containing the 3 last searched words. So I'm using 3 Session Variables: Word1 / Word2 / Word3. In order to get this history working, I need to put the...
9
by: Nathan Sokalski | last post by:
I am trying to do a database search using LIKE using the following code: Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click If...
7
by: Ron | last post by:
I have a couple of questions about this class.....: Public Class WordPair Private word1 As String Private word2 As String Public Sub New() word1 = "" word2 = "" End Sub Public Sub New(ByVal...
11
by: Flyzone | last post by:
Hello, i have again problem with regexp :-P I need to match all lines that contain one word but not contain another. Like to do "grep one | grep -v two:" The syntax of the string is: (any...
6
by: logieen | last post by:
Hi , Every one I really need some help in my programs it doesn't work as I want I know there something wrong in it if you know where the wrong is just show me plz as soon as Possible plzzzzz. I a...
11
by: captainphoenix | last post by:
In VB (.NET?) building a query from a table made in MS Access. I'm trying to type the sql line WHERE = @ but it won't recognize the code because "Course Number" is two words. the whole thing...
3
by: Edwin.Madari | last post by:
sounds like *soundex* is what you are looking for. google soundex regards Edwin -----Original Message----- From: python-list-bounces+edwin.madari=verizonwireless.com@python.org On Behalf...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.