443,846 Members | 1,862 Online
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

# strange for loop construct

 P: n/a I was googling for an example of the classic word frequency program in Python as I'm just learning the language, and wanted to see how other people implemented it. I found this blog post http://digitalhistory.uwo.ca/dhh/ind...d-frequencies/ (with a much more concise version than I managed) but I can't seem to find any mention in various Python documentation of the following construct wordfreq = [wordlist.count(p) for p in wordlist] I would expect for p in wordlist: wordfreq.append(wordlist.count(p)) I didn't know you could have an expression in the same line. Jan 5 '07 #1
9 Replies

 P: n/a At Friday 5/1/2007 17:39, Sa********@gmail.com wrote: >wordfreq = [wordlist.count(p) for p in wordlist]I would expectfor p in wordlist: wordfreq.append(wordlist.count(p)) I didn't know you could have an expression in the same line. That's known as a "list comprehension" and is roughly equivalent to your code. Section 5 of the tutorial covers them. http://docs.python.org/tut/node7.html -- Gabriel Genellina Softlab SRL __________________________________________________ Preguntá. Respondé. Descubrí. Todo lo que querías saber, y lo que ni imaginabas, está en Yahoo! Respuestas (Beta). ¡Probalo ya! http://www.yahoo.com.ar/respuestas Jan 5 '07 #2

 P: n/a * Gabriel Genellina wrote (on 1/5/2007 12:49 PM): At Friday 5/1/2007 17:39, Sa********@gmail.com wrote: >wordfreq = [wordlist.count(p) for p in wordlist]I would expectfor p in wordlist: wordfreq.append(wordlist.count(p)) I didn't know you could have an expression in the same line. That's known as a "list comprehension" and is roughly equivalent to your code. Section 5 of the tutorial covers them. http://docs.python.org/tut/node7.html If you have a Python installation you should be able to find the "Whats New" section of the docs. List comprehensions are described pretty well in the "What's new in Python 2.0?" section. This gives some simple examples as well as the rationale behind them. Mark Jan 5 '07 #3

 P: n/a Mark Elston ha escrito: If you have a Python installation you should be able to find the "Whats New" section of the docs. List comprehensions are described pretty well in the "What's new in Python 2.0?" section. This gives some simple examples as well as the rationale behind them. Where do you find the "What's new" for previous releases? I have to read them online. -- Gabriel Genellina Jan 7 '07 #4

 P: n/a GabrielWhere do you find the "What's new" for previous releases? I Gabrielhave to read them online. Google for what's new site:python.org Skip Jan 7 '07 #5

 P: n/a On 7 ene, 16:34, s...@pobox.com wrote: GabrielWhere do you find the "What's new" for previous releases? I Gabrielhave to read them online. Google for what's new site:python.org That's what I do. But this post: If you have a Python installation you should be able to find the "Whats New" section of the docs. List comprehensions are described pretty well in the "What's new in Python 2.0?" section. suggested that one could find that info inside the Python installation, and I was asking *where*, because I can't find it, and I suspect it actually isn't there. -- Gabriel Genellina Jan 7 '07 #6

 P: n/a GabrielWhere do you find the "What's new" for previous releases? I Gabrielhave to read them online. >>Google for what's new site:python.org Sorry, I took "I have to read them online" to mean that you needed to read them online because (perhaps) you don't have a source distribution on your computer. My 2.5 source (Subversion sandbox) has 2.0 through 2.5 What's New source in Doc/whatsnew. Skip Jan 7 '07 #7

 P: n/a sk**@pobox.com writes: GabrielWhere do you find the "What's new" for previous releases? I Gabrielhave to read them online. >> >Google for what's new site:python.org Sorry, I took "I have to read them online" to mean that you needed to read them online because (perhaps) you don't have a source distribution on your computer. My 2.5 source (Subversion sandbox) has 2.0 through 2.5 What's New source in Doc/whatsnew. My SuSE installation has it as /usr/share/doc/packages/python/Misc/NEWS -- Jorge Godoy Jan 7 '07 #8

 P: n/a Mark Elston wrote: * Gabriel Genellina wrote (on 1/5/2007 12:49 PM): At Friday 5/1/2007 17:39, Sa********@gmail.com wrote: wordfreq = [wordlist.count(p) for p in wordlist] I would expect for p in wordlist: wordfreq.append(wordlist.count(p)) I didn't know you could have an expression in the same line. That's known as a "list comprehension" and is roughly equivalent to your code. Section 5 of the tutorial covers them. http://docs.python.org/tut/node7.html If you have a Python installation you should be able to find the "Whats New" section of the docs. List comprehensions are described pretty well in the "What's new in Python 2.0?" section. This gives some simple examples as well as the rationale behind them. Shouldn't that same page be found on the python website? http://www.python.org/doc/2.0/ Any clue as to why it isn't? Mark Jan 7 '07 #9

 P: n/a On 7 ene, 18:52, "Dustan"

### This discussion thread is closed

Replies have been disabled for this discussion.