Re: strange for loop construct
- From: Mark Elston <m.elston@xxxxxxxxxxxxxxxxx>
- Date: Fri, 05 Jan 2007 13:05:02 -0800
* Gabriel Genellina wrote (on 1/5/2007 12:49 PM):
At Friday 5/1/2007 17:39, Sardaukary@xxxxxxxxx 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.
Mark
.
- Follow-Ups:
- Re: strange for loop construct
- From: Dustan
- Re: strange for loop construct
- From: Gabriel Genellina
- Re: strange for loop construct
- References:
- strange for loop construct
- From: Sardaukary
- Re: strange for loop construct
- From: Gabriel Genellina
- strange for loop construct
- Prev by Date: Re: Problem Running Working Code on Mac
- Next by Date: Re: MySQLdb windows binaries for Python 2.5?? Yes, but from a World of Warcraft guild.
- Previous by thread: Re: strange for loop construct
- Next by thread: Re: strange for loop construct
- Index(es):
Relevant Pages
|