learning with python question (HtTLaPP)

2008-04-26 Thread umpsumps
Hello all,

I've been trying to teach myself python from "How to Think Like a
Python Programmer" and have been trying to write a script that checks
'words.txt' for parameters (letters) given.  The problem that is the i
can only get results for the exact sequnce of parameter 'letters'.
I'll spare posting all the different ways I've tried to search for
specific letters.  But they are all generally:

for line in fin:
for linechar in line:
for ch in letters:

or the "for linechar in line:" and "for ch in letters:" get switched..
I'm getting really frustrated to say the least.

 What alternative method could I use that isn't too advanced?  Any
tips/suggestions on the code itself would be greatly appreciated, and
tips for learning in general.

here is the code that returns a certain sequence:

>>> def searchtxt(letters):
fin = open('words.txt')
words = ""
index = 0
count = 0
for line in fin:
index +=1
if letters in line.strip():
count += 1
words = line.strip() + '\n' + words

print words
print index, 'lines searched..', count, letters, 'words present'

Thank you in advance.
--
http://mail.python.org/mailman/listinfo/python-list


Re: learning with python question (HtTLaPP)

2008-04-26 Thread umpsumps
Eric,

Thank you for helping.

Is the way I wrote the function inherently wrong?  What I wrote
returns the sequence, however I'm trying to make the output match for
the letters in the string entered, not necessarily the string
sequence.  For example if I search words.txt with my function for
'uzi' I get this:

>>> searchtxt('uzi')
gauziest
gauzier
fuzing
fuzils
fuzil
frouziest
frouzier
defuzing

113809 lines searched.. 8 uzi words present


Only the sequence shows up 'uzi'.  I don't get words like 'unzip' or
'Zurich' .  I've only barely started on invocation and maybe writing
something like I'm describing is above what level I'm currently at.




On Apr 26, 2:20 pm, "Eric Wertman" <[EMAIL PROTECTED]> wrote:
> >  Python Programmer" and have been trying to write a script that checks
> >  'words.txt' for parameters (letters) given.  The problem that is the i
> >  can only get results for the exact sequence of parameter 'letters'.
>
> The "re" module comes to mind:
>
> text = open('words.txt','r').read()
> letters = 'sequence'
> results = re.findall(letters,text)
>
> result_count = len(results)
>
> # one word per line:
> for result in results :
>     print result
>
> # one line
> print ' '.join(results)
>
> of course, you may need to invest a little time in regular expression
> syntax to get exactly what you want, but I think you'll find that's
> not wasted effort, as this is pretty standard and used in a lot of
> other places.

--
http://mail.python.org/mailman/listinfo/python-list


Re: learning with python question (HtTLaPP)

2008-04-26 Thread umpsumps
This is what I'm stuck on.  I keep doing things like:

for line in fin:
for ch in letters:
if ch not in line:


I've tried

for ch in letters:
for line in fin:

too..

Should I use a while statement?  What's the best way to compare a
group of letters to a line?


> This would be a more difficult approach..   Where you are doing the
> comparison step:
>
> if letters in line.strip():
>
> It's trying to match the exact string "uzi", not any of the individual
> letters.  You would need to look for each letter independently and
> then make sure they were in the right order to match the other words.

--
http://mail.python.org/mailman/listinfo/python-list


Re: learning with python question (HtTLaPP)

2008-04-26 Thread umpsumps
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


On Apr 26, 4:02 pm, "Eric Wertman" <[EMAIL PROTECTED]> wrote:
> >  Is the way I wrote the function inherently wrong?  What I wrote
>
> I would not say that.  I think a lot of people probably start off like
> that with python.  You'll find in most cases that manually keeping
> counters isn't necessary.  If you really want to learn python though,
> I would suggest using built in functions and libraries as much as
> possible, as that's where the real power comes from (IMO).
>
> >  returns the sequence, however I'm trying to make the output match for
> >  the letters in the string entered, not necessarily the string
> >  sequence.
> >  Only the sequence shows up 'uzi'.  I don't get words like 'unzip' or
> >  'Zurich' .  I've only barely started on invocation and maybe writing
> >  something like I'm describing is above what level I'm currently at.
>
> This would be a more difficult approach..   Where you are doing the
> comparison step:
>
> if letters in line.strip():
>
> It's trying to match the exact string "uzi", not any of the individual
> letters.  You would need to look for each letter independently and
> then make sure they were in the right order to match the other words.

--
http://mail.python.org/mailman/listinfo/python-list


letter frequency counter / your thoughts..

2008-05-07 Thread umpsumps
Hello,

Here is my code for a letter frequency counter.  It seems bloated to
me and any suggestions of what would be a better way (keep in my mind
I'm a beginner) would be greatly appreciated..

def valsort(x):
res = []
for key, value in x.items():
res.append((value, key))
return res

def mostfreq(strng):
dic = {}
for letter in strng:
if letter not in dic:
dic.setdefault(letter, 1)
else:
dic[letter] += 1
newd = dic.items()
getvals = valsort(newd)
getvals.sort()
length = len(getvals)
return getvals[length - 3 : length]

thanks much!!
--
http://mail.python.org/mailman/listinfo/python-list


Re: letter frequency counter / your thoughts..

2008-05-07 Thread umpsumps
That's a great suggestion Arnaud.  I'll keep that in mind next time I
post code. Thanks ;)


On May 7, 12:27 pm, Arnaud Delobelle <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] writes:
> > Hello,
>
> > Here is my code for a letter frequency counter.  It seems bloated to
> > me and any suggestions of what would be a better way (keep in my mind
> > I'm a beginner) would be greatly appreciated..
>
> > def valsort(x):
> >res = []
> >for key, value in x.items():
> >res.append((value, key))
> >return res
>
> > def mostfreq(strng):
> >dic = {}
> >for letter in strng:
> >if letter not in dic:
> >dic.setdefault(letter, 1)
> >else:
> >dic[letter] += 1
> >newd = dic.items()
> >getvals = valsort(newd)
> >getvals.sort()
> >length = len(getvals)
> >return getvals[length - 3 : length]
>
> > thanks much!!
>
> I won't comment on the algorithm, but I think you should try to find
> better names for your variables.  In the snippet above you have x,
> res, dic, newd, length, getvals which don't give much of a clue as to
> what they are used for.
>
> e.g.
>
> * dic = {}
> We know it's a dict, but a dict of what?
>
> * newd = dic.items()
> Sounds like 'new dictionary', but obviously isn'tas it is a list
> of key,value pairs.
>
> * length = len(getvals)
> Again, we know it's a length, but the length of what?
>
> HTH
>
> --
> Arnaud

--
http://mail.python.org/mailman/listinfo/python-list


Re: anagram finder / dict mapping question

2008-05-09 Thread umpsumps
On May 9, 1:45 am, [EMAIL PROTECTED] wrote:
> >>> key = ''.join(sorted(word))
>
> I tend to strip and lower the word as well, otherwise "Hello" and
> "hello" do not compare...depends on what you want though!
> Plus you might get a lot of "word\n" as keys...
>
> My technique is the this way
>
> def anagram_finder(words):
> anagrams = {}
> for word in words:
> word = word.strip()
> key = ''.join(sorted(word.lower()))
> anagrams.setdefault(key, []).append(word)
> return anagrams

What would be the best method to print the top results, the one's that
had the highest amount of anagrams??  Create a new histogram dict?
--
http://mail.python.org/mailman/listinfo/python-list


Re: anagram finder / dict mapping question

2008-05-09 Thread umpsumps
> > What would be the best method to print the top results, the one's that
> > had the highest amount of anagrams??  Create a new histogram dict?
>
> You can use the max() function to find the biggest list of anagrams:
>
> top_results = max(anagrams.itervalues(), key=len)
>
> --
> Arnaud

That is the biggest list of anagrams, what if I wanted the 3 biggest
lists?  Is there a way to specific top three w/ a max command??

--
http://mail.python.org/mailman/listinfo/python-list