[Tutor] fine in interpreter, hangs in batch
malaclypse2 at gmail.com
Fri Mar 16 20:51:41 CET 2007
On 3/16/07, Switanek, Nick <nswitanek at stanford.edu> wrote:
> After creating a list of words ('wordlist'), I can run the following
> code in the interactive window of PythonWin in about ten seconds. If I
> run the script containing the code, the script seems to hang on the
> loop. I'd be grateful for help as to why; I often seem to have something
> that works in the interpreter, but not when I run the script.
I'm not sure what you mean by 'seems to hang'. The code that you
posted isn't complete enough to run (since you didn't provide a
definition of wordlist), and just generates a NameError exception.
Beyond that, I don't understand what the code is supposed to produce
for output. As written, you generate a list in your loop and assign
it to the name ngrams, but never do anything with that list. Since
you're inside a for loop, your ngrams name is overwritten the next
time you run through the loop. You also generate a string with the
statement "Finished the %d-gram list." % n, but you don't do anything
with it. You probably want to either print it, or assign it to a
variable to print later.
Something like this:
wordlist = ['apple', 'orange', 'pear', 'banana', 'coconut']
N = [2,3,4,5]
ngramlist = [wordlist]
for n in N:
ngrams = [' '.join(wordlist[i:i+n]) for i in range(len(wordlist)-n+1)]
print "Finished the %d-gram list." % n
produces the following output when run as a script:
Finished the 2-gram list.
['apple orange', 'orange pear', 'pear banana', 'banana coconut']
Finished the 3-gram list.
['apple orange pear', 'orange pear banana', 'pear banana coconut']
Finished the 4-gram list.
['apple orange pear banana', 'orange pear banana coconut']
Finished the 5-gram list.
['apple orange pear banana coconut']
Does that help?
More information about the Tutor