Re: for x,y in word1, word2 ?
On Sun, 10 Aug 2008 21:40:55 -0700, Mensanator wrote: On Aug 10, 11:18�pm, ssecorp [EMAIL PROTECTED] wrote: 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: for x,y in word1, word2 ?
ssecorp [EMAIL PROTECTED] writes: 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: for x,y in word1, word2 ?
On Aug 11, 6:40 am, Mensanator [EMAIL PROTECTED] wrote: On Aug 10, 11:18 pm, ssecorp [EMAIL PROTECTED] wrote: 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: for x,y in word1, word2 ?
On Aug 11, 5:40 am, Mensanator [EMAIL PROTECTED] wrote: On Aug 10, 11:18 pm, ssecorp [EMAIL PROTECTED] wrote: 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: for x,y in word1, word2 ?
On Aug 10, 11:14 pm, ssecorp [EMAIL PROTECTED] wrote: On Aug 11, 6:40 am, Mensanator [EMAIL PROTECTED] wrote: On Aug 10, 11:18 pm, ssecorp [EMAIL PROTECTED] wrote: 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: for x,y in word1, word2 ?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: for x,y in word1, word2 ?
sounds like *soundex* is what you are looking for. google soundex regards Edwin -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Marc 'BlackJack' Rintsch Sent: Monday, August 11, 2008 3:09 AM To: python-list@python.org Subject: Re: for x,y in word1, word2 ? 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 -- http://mail.python.org/mailman/listinfo/python-list The information contained in this message and any attachment may be proprietary, confidential, and privileged or subject to the work product doctrine and thus protected from disclosure. If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify me immediately by replying to this message and deleting it and all copies and backups thereof. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: for x,y in word1, word2 ?
My first thought is that you should be looking at implementations of Hamming Distance. If you are actually looking for something like SOUNDEX you might also want to look at the double metaphor algorithm, which is significantly harder to implement but provides better matching and is less susceptible to differences based on name origins. -- http://mail.python.org/mailman/listinfo/python-list
Re: for x,y in word1, word2 ?
On Mon, Aug 11, 2008 at 8:44 AM, Casey [EMAIL PROTECTED] wrote: My first thought is that you should be looking at implementations of Hamming Distance. If you are actually looking for something like SOUNDEX you might also want to look at the double metaphor algorithm, which is significantly harder to implement but provides better matching and is less susceptible to differences based on name origins. -- http://mail.python.org/mailman/listinfo/python-list I responded in the thread of the poster's original message on this subject, but will do the same here. I have a horribly ugly version of the double-metaphone algorithm in python that does work, and may be of some use in solving this problem. -- Stand Fast, tjg. [Timothy Grant] -- http://mail.python.org/mailman/listinfo/python-list
Re: for x,y in word1, word2 ?
Thanks, Timothy. I'm pretty sure that there is no such thing as a beautiful implementation of double-metaphone but I would personally like to have a copy of your python implementation. I have a fairly elegant version of the original metaphone algorithm I wrote myself (in PERL, many years ago) but I've never found the time to reverse-engineer the original C++ code for double-metaphone and pythonize it. On Mon, Aug 11, 2008 at 2:08 PM, Timothy Grant [EMAIL PROTECTED] wrote: On Mon, Aug 11, 2008 at 8:44 AM, Casey [EMAIL PROTECTED] wrote: My first thought is that you should be looking at implementations of Hamming Distance. If you are actually looking for something like SOUNDEX you might also want to look at the double metaphor algorithm, which is significantly harder to implement but provides better matching and is less susceptible to differences based on name origins. -- http://mail.python.org/mailman/listinfo/python-list I responded in the thread of the poster's original message on this subject, but will do the same here. I have a horribly ugly version of the double-metaphone algorithm in python that does work, and may be of some use in solving this problem. -- Stand Fast, tjg. [Timothy Grant] -- http://mail.python.org/mailman/listinfo/python-list
Re: for x,y in word1, word2 ?
On Mon, Aug 11, 2008 at 12:13 PM, Dave Webster [EMAIL PROTECTED] wrote: Thanks, Timothy. I'm pretty sure that there is no such thing as a beautiful implementation of double-metaphone but I would personally like to have a copy of your python implementation. I have a fairly elegant version of the original metaphone algorithm I wrote myself (in PERL, many years ago) but I've never found the time to reverse-engineer the original C++ code for double-metaphone and pythonize it. On Mon, Aug 11, 2008 at 2:08 PM, Timothy Grant [EMAIL PROTECTED] wrote: On Mon, Aug 11, 2008 at 8:44 AM, Casey [EMAIL PROTECTED] wrote: My first thought is that you should be looking at implementations of Hamming Distance. If you are actually looking for something like SOUNDEX you might also want to look at the double metaphor algorithm, which is significantly harder to implement but provides better matching and is less susceptible to differences based on name origins. -- http://mail.python.org/mailman/listinfo/python-list I responded in the thread of the poster's original message on this subject, but will do the same here. I have a horribly ugly version of the double-metaphone algorithm in python that does work, and may be of some use in solving this problem. -- Stand Fast, tjg. [Timothy Grant] This is truly cringe-worthy, and pretty much a direct port of the C++ code. It need unit tests (which are on my to-do someday list) but even though it's ugly it does work and I have managed to do real work with it. -- Stand Fast, tjg. [Timothy Grant] # # DMetaph.py # # Copyright 2006 by Timothy (rhacer) Grant # # Based on an algorithm and code by Lawrence Philips # # This code is licensed under the terms of the GNU Public License v2 # import re DEBUG = False class DMetaph(str): DMetaph(word) creates a Double Metaphone encoding of the word passed in. def __init__(self, s): self.s = s.upper() + ' ' # Padding allows safe indexing beyond the end self.length = len(s) self.last = self.length - 1 self.current = 0 self.primary = self.secondary = self.alternate = False if self.StringAt(0, 2, 'GN', 'KN', 'PN', 'WR', 'PS'): self.current += 1 if self.s[0] == 'X': self.MetaphAdd('S') self.current += 1 while len(self.primary) 4 or len(self.secondary) 4: if DEBUG: print processing character: %s current: %s length: %s % ( self.s[self.current], self.current, self.length) if self.current = self.length: break self.ProcessString() self.metaph = self.primary self.metaph2 = '' if len(self.metaph) 4: self.metaph = self.metaph[:4] if self.alternate: self.metaph2 = self.secondary if len(self.metaph2) 4: self.metaph2 = self.metaph2[:4] def SlavoGermanic(self): if ('W' in self.s or 'K' in self.s or 'CZ' in self.s or 'WITZ' in self.s): return True return False def MetaphAdd(self, main, alt=None): if main: self.primary += main if alt: self.alternate = True if (alt[0] ' '): self.secondary += alt else: if (main[0] ' '): self.secondary += main def IsVowel(self, at): if (at 0) or (at self.length): return False if self.s[at] in 'AEIOUY': return True return False def StringAt(self, start, length, *sub_strings): if self.s[start:start + length] in sub_strings: return True return False def print_diags(self, section): printsection: %s current: %s % (section, self.current) def ProcessString(self): if self.IsVowel(self.current): if DEBUG: self.print_diags('VOWEL') if self.current == 0: self.MetaphAdd('A') self.current += 1 return elif self.s[self.current] == 'B': if DEBUG: self.print_diags('B') self.MetaphAdd('P') if self.s[self.current + 1] == 'B': self.current += 2 else: self.current += 1 return elif self.s[self.current] == 'C': if DEBUG: self.print_diags('C') if (self.current 1 and not self.IsVowel(self.current - 2) and self.StringAt(self.current - 1, 3, 'ACH') and self.s[self.current + 2] 'I' and self.s[self.current + 2] 'E' or
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: -- http://mail.python.org/mailman/listinfo/python-list
Re: for x,y in word1, word2 ?
On Aug 10, 11:18�pm, ssecorp [EMAIL PROTECTED] wrote: 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)] -- http://mail.python.org/mailman/listinfo/python-list