mirror of
https://github.com/donnemartin/data-science-ipython-notebooks.git
synced 2024-03-22 13:30:56 +08:00
Added snippet for generator expressions.
This commit is contained in:
parent
97c7118117
commit
7c5609f1e2
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"metadata": {
|
||||
"name": "",
|
||||
"signature": "sha256:a81c81558508aed1c6f336342652e5f8dcc250ecd9f9f955b9e6e02d65a45c5c"
|
||||
"signature": "sha256:d9c538ef6dad540e5963356ed91d625e86a9a447c42edc8d3fe3a76031768782"
|
||||
},
|
||||
"nbformat": 3,
|
||||
"nbformat_minor": 0,
|
||||
|
@ -452,8 +452,8 @@
|
|||
"collapsed": false,
|
||||
"input": [
|
||||
"def squares(n=5):\n",
|
||||
" for i in xrange(1, n + 1):\n",
|
||||
" yield i ** 2\n",
|
||||
" for x in xrange(1, n + 1):\n",
|
||||
" yield x ** 2\n",
|
||||
"\n",
|
||||
"# No code is executed\n",
|
||||
"gen = squares()\n",
|
||||
|
@ -478,6 +478,40 @@
|
|||
}
|
||||
],
|
||||
"prompt_number": 10
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Generator Expressions\n",
|
||||
"\n",
|
||||
"A generator expression is analogous to a comprehension. A list comprehension is enclosed by [], a generator expression is enclosed by ():"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"gen = (x ** 2 for x in xrange(1, 6))\n",
|
||||
"for x in gen:\n",
|
||||
" print x"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"1\n",
|
||||
"4\n",
|
||||
"9\n",
|
||||
"16\n",
|
||||
"25\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 11
|
||||
}
|
||||
],
|
||||
"metadata": {}
|
||||
|
|
Loading…
Reference in New Issue
Block a user