problem with dictionaries
Terry Reedy
tjreedy at udel.edu
Wed Apr 23 15:40:12 EDT 2008
"Simon Strobl" <Simon.Strobl at gmail.com> wrote in message
news:ed249bcd-87ae-48ab-9aa9-c4bc9177b5c4 at b64g2000hsa.googlegroups.com...
| Any hints?
For future reference, I would consider using sample data in the file itself
for debugging -- to separate an algorithm problem from a file problem:
| ================================================
|
| #!/usr/bin/python
|
| import sys
|
| frqlist = open('my_frqlist.txt', 'r')
|
| # my_frqlist looks like this:
| # 787560608|the
| # 434879575|of
| # 413442185|and
| # 395209748|to
| # 284833918|a
| # 249111541|in
| # 169988976|is
Change above to:
# frqlist = open('my_frqlist.txt', 'r')
frqlist = '''\
787560608|the
434879575|of
413442185|and
395209748|to
284833918|a
249111541|in
169988976|is'''.split('\n') # sample my_frqlist.txt
| frq = {}
|
| for line in frqlist:
| line = line.rstrip()
| frequency, word = line.split('|')
| frq[word] = int(frequency)
|
| for key in frq.keys():
| print key, frq[key]
An alternative is 'print line' in the first loop after stripping.
tjr
More information about the Python-list
mailing list