Re: [Tutor] Trying to enter text from a file to a Dictionary
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 even then I'm not sure that is easier to read than z = [] for x in L: z.append(x*2) And personally I still find map() easier for those cases z = map(lambda x: x*2, L) But you do need to be comfortable with lambda for that, and lambda is just as hard to grok as list comprehensions! :-) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trying to enter text from a file to a Dictionary
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 like z = [x*2 for x in L] and even then I'm not sure that is easier to read than z = [] for x in L: z.append(x*2) 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 will think, I need a list of the squares of everything in L. This matches exactly the order of elements in a list comp. Personally I avoid using list comps purely for the side effects, to me that breaks the conceptual I need a list I admit that some of the most unreadable one-liners on comp.lang.python use list comps in creative ways...I write out the loop rather than going to contortions to make an expression I can use in a list comp. I don't see the point in twistng the code to fit it into a list comp. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trying to enter text from a file to a Dictionary
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 about the syntax, that my intuition resists. Once I got it, though -- after maybe three attempts -- it stuck very well, and I like them better than their equivalent for-loops. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trying to enter text from a file to a Dictionary
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: gloss[line] = line The problem that you have is that you really need to read two lines at a time. (Assuming that the definitions are all on one line which may not be true!) A while loop may be easier in this case. A for loop will read each line individually. You then need to set a definition flag to tell the loop body whether you are reading a definition or a key. Either type of loop is possible. Since you started with a for loop lets stick with it... definition = False currentKey = None for line in f: if isDefinition: gloss[currentKey] = line currentKey = None isDefinition = False else: currentKey = line isDefinition = True If the definitions take up more than one line each then you will need to think about how to identify the end of a definition - a blank line maybe? You will need to append the lines to the glossary entry (rather than just assign them) until the end of the definition. And you will need to update the isDefinition flag at the appropriate times. HTH, Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trying to enter text from a file to a Dictionary
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('glossary.txt','r') gloss = {} for line in f: gloss[line] = line The problem that you have is that you really need to read two lines at a time. (Assuming that the definitions are all on one line which may not be true!) A while loop may be easier in this case. A for loop will read each line individually. You then need to set a definition flag to tell the loop body whether you are reading a definition or a key. Either type of loop is possible. Since you started with a for loop lets stick with it... definition = False currentKey = None 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() If the definitions take up more than one line each then you will need to think about how to identify the end of a definition - a blank line maybe? You will need to append the lines to the glossary entry (rather than just assign them) until the end of the definition. And you will need to update the isDefinition flag at the appropriate times. HTH, Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trying to enter text from a file to a Dictionary
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. I tried using a for loop like this f = open('glossary.txt','r') gloss = {} for line in f: gloss[line] = line The problem that you have is that you really need to read two lines at a time. (Assuming that the definitions are all on one line which may not be true!) A while loop may be easier in this case. A for loop will read each line individually. You then need to set a definition flag to tell the loop body whether you are reading a definition or a key. Either type of loop is possible. Since you started with a for loop lets stick with it... definition = False currentKey = None 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() Or even: [gloss.setdefault(l,f.next()) for l in f] If the definitions take up more than one line each then you will need to think about how to identify the end of a definition - a blank line maybe? You will need to append the lines to the glossary entry (rather than just assign them) until the end of the definition. And you will need to update the isDefinition flag at the appropriate times. HTH, Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trying to enter text from a file to a Dictionary
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 you can. I'd forgotten about that neat feature of iterators. No longer are for loops broken when you mess around with the iterated item. Good catch Bob. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trying to enter text from a file to a Dictionary
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 thetext file into the dict. The way the text file is set up is that oneline is the word and the next line is the definition.I tried using a for loop like thisf = open('glossary.txt','r')gloss = {} for line in f:gloss[line] = line The problem that you have is that you really need to read two lines at a time. (Assuming that the definitions are all on one line which may not be true!) A while loop may be easier in this case. A for loop will read each line individually. You then need to set a definition flag to tell the loop body whether you are reading a definition or a key. Either type of loop is possible. Since you started with a for loop lets stick with it... definition = False currentKey = None for line in f: if isDefinition:gloss[currentKey] = linecurrentKey = None isDefinition = False else:currentKey = lineisDefinition = True Or you can use next(): for line in f: gloss[line] = f.next()Or even:[gloss.setdefault(l,f.next()) for l in f] Hello Bob I understand f.next(), but [gloss.setdefault(l,f.next()) for l in f] is beyond me at this point. Thanks for your input. Ben ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trying to enter text from a file to a Dictionary
Ben Markwell wrote: On 1/27/06, *Bob Gailer* [EMAIL PROTECTED] mailto:[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. I tried using a for loop like this f = open('glossary.txt','r') gloss = {} for line in f: gloss[line] = line The problem that you have is that you really need to read two lines at a time. (Assuming that the definitions are all on one line which may not be true!) A while loop may be easier in this case. A for loop will read each line individually. You then need to set a definition flag to tell the loop body whether you are reading a definition or a key. Either type of loop is possible. Since you started with a for loop lets stick with it... definition = False currentKey = None 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() Or even: [gloss.setdefault(l,f.next()) for l in f] Hello Bob 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 that may involve l. result = [expr for l in f] # is equivalent to: result = [] for l in f: result.append(expr) list comprehension (once understood) is often easier to read and more efficient than the for loop. result = xxx.setdefault(key, newvalue) is a dictionary method that tries to get an item from the dictionary xxx using key. If the key is not in the dictionary it adds the item assigning it newvalue. Equivalent to: if key not in xxx: xxx[key] = newvalue result = xxx[key] Note that list comprehension and setdefault both return something. In my code the returned values are ignored. The outcome (populating a dictionary via a loop) is a side effect. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trying to enter text from a file to a Dictionary
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 (once understood) is often easier to read and moreefficient than the for loop.result = xxx.setdefault(key, newvalue) is a dictionary method that triesto get an item from the dictionary xxx using key. If the key is not in the dictionary it adds the item assigning it newvalue. Equivalent to:if key not in xxx:xxx[key] = newvalueresult = xxx[key]Note that list comprehension and setdefault both return something. In my code the returned values are ignored. The outcome (populating adictionary via a loop) is a side effect. Thank you Bob. This is much to think about. I very much appreciate your concise explanation. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trying to enter text from a file to a Dictionary
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 efficiency reasons, the iterator's allowed to march through the file ahead several lines at with an internal buffer. This means our position in the file might be further along than we might realize, and that means that readline() will give nonsensical results. So we're getting caught by a low-level detail. We should try to avoid using both the for loop and readline() on the same file. Here's one way we can avoid the problem: while True: word = f.readline() defn = f.readline() if not word or not defn: break ... Does this make sense? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor