defaultdict

This commit is contained in:
rasbt 2014-05-01 21:37:42 -04:00
parent 4d97ce01f2
commit 2fca2d34d7
2 changed files with 2 additions and 52 deletions

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:1dc4a14b00afc36bd728a4f8d00ea951e5c9376232218e2e5a36e653e875fbe5"
"signature": "sha256:75d807f509bd9f76b2e14a5a048cb44852a3318bcd0d95afc95d1c9b2904c078"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -1061,31 +1061,6 @@
],
"prompt_number": 25
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def add_element_get(elements):\n",
" d = dict()\n",
" for e in elements:\n",
" d[e] = d.get(e, 1) + 1\n",
" return d\n",
"\n",
"print(add_element_get(rand_ints))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{1: 17, 2: 10, 3: 11, 4: 7, 5: 9, 6: 14, 7: 13, 8: 13, 9: 15}\n"
]
}
],
"prompt_number": 24
},
{
"cell_type": "markdown",
"metadata": {},

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:1dc4a14b00afc36bd728a4f8d00ea951e5c9376232218e2e5a36e653e875fbe5"
"signature": "sha256:75d807f509bd9f76b2e14a5a048cb44852a3318bcd0d95afc95d1c9b2904c078"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -1061,31 +1061,6 @@
],
"prompt_number": 25
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def add_element_get(elements):\n",
" d = dict()\n",
" for e in elements:\n",
" d[e] = d.get(e, 1) + 1\n",
" return d\n",
"\n",
"print(add_element_get(rand_ints))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{1: 17, 2: 10, 3: 11, 4: 7, 5: 9, 6: 14, 7: 13, 8: 13, 9: 15}\n"
]
}
],
"prompt_number": 24
},
{
"cell_type": "markdown",
"metadata": {},