Re: [Tutor] Trying to enter text from a file to a Dictionary

2006-01-28 Thread Terry Carroll
On Sat, 28 Jan 2006, Kent Johnson wrote: > I find simple list comps far easier to read and write than the > equivalent for loop, and they fit the way I think about problems... I don't know if my experience is typical, but I found list comprehensions difficult to get. There's something, maybe ab

Re: [Tutor] Trying to enter text from a file to a Dictionary

2006-01-28 Thread Kent Johnson
Alan Gauld wrote: > Hi Bob, > > >>"list comprehension" (once understood) is often easier to read and more >>efficient than the for loop. > > > They are often more efficient but I don't know if I'd ever claim they were > easier to read than an explicit for loop. Perhaps the most trivial cases

Re: [Tutor] Trying to enter text from a file to a Dictionary

2006-01-28 Thread Alan Gauld
Hi Bob, > "list comprehension" (once understood) is often easier to read and more > efficient than the for loop. They are often more efficient but I don't know if I'd ever claim they were easier to read than an explicit for loop. Perhaps the most trivial cases like z = [x*2 for x in L] and

Re: [Tutor] Trying to enter text from a file to a Dictionary

2006-01-27 Thread Ben Markwell
> I understand f.next(), but [gloss.setdefault(l,f.next()) for l in f]> is beyond me at this point. [expr for l in f] is a "list comprehension". expr is an _expression_ thatmay involve l.result = [expr for l in f] # is equivalent to:result = []for l in f:result.append(expr) "list comprehension"

Re: [Tutor] Trying to enter text from a file to a Dictionary

2006-01-27 Thread Bob Gailer
Ben Markwell wrote: > > > On 1/27/06, *Bob Gailer* <[EMAIL PROTECTED] > > wrote: > > Bob Gailer wrote: > > Alan Gauld wrote: > > > >> Hi Ben, > >> > >> > >> > >>> I want to enter the words and definitions from the text file > into the

Re: [Tutor] Trying to enter text from a file to a Dictionary

2006-01-27 Thread Ben Markwell
On 1/27/06, Bob Gailer <[EMAIL PROTECTED]> wrote: Bob Gailer wrote:> Alan Gauld wrote:>>> Hi Ben,> I want to enter the words and definitions from the  text file into the>>> dict.>>> The way the text file is set up is that one  line is the word and the >>> next line is the definition.>>>

Re: [Tutor] Trying to enter text from a file to a Dictionary

2006-01-27 Thread Alan Gauld
>> for line in f: >> if isDefinition: >>gloss[currentKey] = line >>currentKey = None >>isDefinition = False >> else: >>currentKey = line >>isDefinition = True >> > Or you can use next(): > > for line in f: >gloss[line] = f.next() Ah! Indeed y

Re: [Tutor] Trying to enter text from a file to a Dictionary

2006-01-27 Thread Bob Gailer
Bob Gailer wrote: > Alan Gauld wrote: > >> Hi Ben, >> >> >> >>> I want to enter the words and definitions from the text file into the >>> dict. >>> The way the text file is set up is that one line is the word and the >>> next line is the definition. >>> >>> >> >> >

Re: [Tutor] Trying to enter text from a file to a Dictionary

2006-01-27 Thread Bob Gailer
Alan Gauld wrote: > Hi Ben, > > >> I want to enter the words and definitions from the text file into the >> dict. >> The way the text file is set up is that one line is the word and the >> next line is the definition. >> > > >> I tried using a for loop like this >> >> f = open('glos

Re: [Tutor] Trying to enter text from a file to a Dictionary

2006-01-27 Thread Alan Gauld
Hi Ben, > I want to enter the words and definitions from the text file into the > dict. > The way the text file is set up is that one line is the word and the > next line is the definition. > I tried using a for loop like this > > f = open('glossary.txt','r') > gloss = {} > > for line in f

Re: [Tutor] Trying to enter text from a file to a Dictionary

2006-01-26 Thread Danny Yoo
> I tried : > > for line in f: > gloss[line] = f.readline() > This should have worked, but there's one problem. Whenever we're doing something like: for line in f: ... there can be some interference between the iteration and any readline() in the body of the loop. For eff

[Tutor] Trying to enter text from a file to a Dictionary

2006-01-26 Thread Ben Markwell
Being new to programming, I made a text file that contains terms with their definitions that I have come across in my studying. As an exercise, I thought  I would make a glossary using a dictionary so I can look up words, add new words, view all entries, etc.  I want to enter the words and def