learning with python question (HtTLaPP)

Eric Wertman ewertman at gmail.com
Sun Apr 27 03:39:50 CEST 2008


On Sat, Apr 26, 2008 at 7:50 PM,  <umpsumps at gmail.com> wrote:
> ok.. I finally made something that works.. Please let me know what you
>  think:
>
>  >>> def lines(letters):
>         fin = open('words.txt')
>         count = 0
>         rescount = 0  # count the number of results
>         results = ""  # there are words that contain the letters
>         for line in fin:
>                 needs = 0
>                 x = str(line.strip())
>                 for ch in letters:
>                         if ch not in x:
>                                 pass
>                         else:
>                                 needs = needs + 1
>                 if needs == len(letters):
>                         rescount += 1
>                         results = results + '\n' + x
>                 count += 1
>         print count, 'lines searched'
>         print results, '\n'
>         print 'result count is: ', rescount


That's pretty much it.. I'm guessing you are assuming your file has
one word per line?  I took a shot at it, without using the regex
module:

file = open('spyware')

my_string = 'uzi'
length = len(my_string)
words = []

for line in file :
    chunks = line.strip().split()
    for chunk in chunks :
        x = 0
        for char in my_string :
            x = chunk.rfind(char,x)
        if x > 0 :
            words.append(chunk)

print '\n'.join(words)


or with the re module:

import re

text = open('words.txt').read()
pattern = '\S*u\S*z\S*i\S*'
stuff = re.findall(pattern,text)
count = len(stuff)

print "Found %d words :" % (count)
print "\n".join(stuff)



More information about the Python-list mailing list