mirror of
https://github.com/rasbt/python_reference.git
synced 2025-01-18 07:17:06 +00:00
defaultdict
This commit is contained in:
parent
409d953a22
commit
4d97ce01f2
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"metadata": {
|
||||
"name": "",
|
||||
"signature": "sha256:8dc4f91bc6a88e15ab0d25fac35b9a7645a7149b5ab4e1e15b2b372362e82ae2"
|
||||
"signature": "sha256:1dc4a14b00afc36bd728a4f8d00ea951e5c9376232218e2e5a36e653e875fbe5"
|
||||
},
|
||||
"nbformat": 3,
|
||||
"nbformat_minor": 0,
|
||||
|
@ -891,6 +891,13 @@
|
|||
" d[e] += 1\n",
|
||||
" return d\n",
|
||||
"\n",
|
||||
"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",
|
||||
"\n",
|
||||
"random.seed(123)\n",
|
||||
"\n",
|
||||
"print('Results for 100 integers in range 1-10') \n",
|
||||
|
@ -899,6 +906,7 @@
|
|||
"%timeit add_element_check2(rand_ints)\n",
|
||||
"%timeit add_element_except(rand_ints)\n",
|
||||
"%timeit add_element_defaultdict(rand_ints)\n",
|
||||
"%timeit add_element_get(rand_ints)\n",
|
||||
"\n",
|
||||
"print('\\nResults for 1000 integers in range 1-5') \n",
|
||||
"rand_ints = [random.randrange(1, 5) for i in range(1000)]\n",
|
||||
|
@ -906,13 +914,15 @@
|
|||
"%timeit add_element_check2(rand_ints)\n",
|
||||
"%timeit add_element_except(rand_ints)\n",
|
||||
"%timeit add_element_defaultdict(rand_ints)\n",
|
||||
"%timeit add_element_get(rand_ints)\n",
|
||||
"\n",
|
||||
"print('\\nResults for 1000 integers in range 1-1000') \n",
|
||||
"rand_ints = [random.randrange(1, 1000) for i in range(1000)]\n",
|
||||
"%timeit add_element_check1(rand_ints)\n",
|
||||
"%timeit add_element_check2(rand_ints)\n",
|
||||
"%timeit add_element_except(rand_ints)\n",
|
||||
"%timeit add_element_defaultdict(rand_ints)"
|
||||
"%timeit add_element_defaultdict(rand_ints)\n",
|
||||
"%timeit add_element_get(rand_ints)"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
|
@ -922,7 +932,7 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"Results for 100 integers in range 1-10\n",
|
||||
"10000 loops, best of 3: 24.6 \u00b5s per loop"
|
||||
"10000 loops, best of 3: 28 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -938,7 +948,7 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"10000 loops, best of 3: 25.4 \u00b5s per loop"
|
||||
"10000 loops, best of 3: 26.5 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -946,7 +956,15 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"10000 loops, best of 3: 23 \u00b5s per loop"
|
||||
"10000 loops, best of 3: 22.8 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"10000 loops, best of 3: 33.3 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -956,7 +974,7 @@
|
|||
"\n",
|
||||
"\n",
|
||||
"Results for 1000 integers in range 1-5\n",
|
||||
"1000 loops, best of 3: 236 \u00b5s per loop"
|
||||
"1000 loops, best of 3: 242 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -964,7 +982,7 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"1000 loops, best of 3: 235 \u00b5s per loop"
|
||||
"1000 loops, best of 3: 239 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -972,7 +990,7 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"1000 loops, best of 3: 207 \u00b5s per loop"
|
||||
"1000 loops, best of 3: 203 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -980,7 +998,15 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"10000 loops, best of 3: 177 \u00b5s per loop"
|
||||
"10000 loops, best of 3: 184 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"1000 loops, best of 3: 350 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -990,7 +1016,7 @@
|
|||
"\n",
|
||||
"\n",
|
||||
"Results for 1000 integers in range 1-1000\n",
|
||||
"1000 loops, best of 3: 268 \u00b5s per loop"
|
||||
"1000 loops, best of 3: 262 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -998,7 +1024,7 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"1000 loops, best of 3: 377 \u00b5s per loop"
|
||||
"1000 loops, best of 3: 370 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -1006,7 +1032,7 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"1000 loops, best of 3: 511 \u00b5s per loop"
|
||||
"1000 loops, best of 3: 502 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -1014,7 +1040,15 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"1000 loops, best of 3: 410 \u00b5s per loop"
|
||||
"1000 loops, best of 3: 422 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"1000 loops, best of 3: 373 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -1025,7 +1059,32 @@
|
|||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 16
|
||||
"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",
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"metadata": {
|
||||
"name": "",
|
||||
"signature": "sha256:8dc4f91bc6a88e15ab0d25fac35b9a7645a7149b5ab4e1e15b2b372362e82ae2"
|
||||
"signature": "sha256:1dc4a14b00afc36bd728a4f8d00ea951e5c9376232218e2e5a36e653e875fbe5"
|
||||
},
|
||||
"nbformat": 3,
|
||||
"nbformat_minor": 0,
|
||||
|
@ -891,6 +891,13 @@
|
|||
" d[e] += 1\n",
|
||||
" return d\n",
|
||||
"\n",
|
||||
"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",
|
||||
"\n",
|
||||
"random.seed(123)\n",
|
||||
"\n",
|
||||
"print('Results for 100 integers in range 1-10') \n",
|
||||
|
@ -899,6 +906,7 @@
|
|||
"%timeit add_element_check2(rand_ints)\n",
|
||||
"%timeit add_element_except(rand_ints)\n",
|
||||
"%timeit add_element_defaultdict(rand_ints)\n",
|
||||
"%timeit add_element_get(rand_ints)\n",
|
||||
"\n",
|
||||
"print('\\nResults for 1000 integers in range 1-5') \n",
|
||||
"rand_ints = [random.randrange(1, 5) for i in range(1000)]\n",
|
||||
|
@ -906,13 +914,15 @@
|
|||
"%timeit add_element_check2(rand_ints)\n",
|
||||
"%timeit add_element_except(rand_ints)\n",
|
||||
"%timeit add_element_defaultdict(rand_ints)\n",
|
||||
"%timeit add_element_get(rand_ints)\n",
|
||||
"\n",
|
||||
"print('\\nResults for 1000 integers in range 1-1000') \n",
|
||||
"rand_ints = [random.randrange(1, 1000) for i in range(1000)]\n",
|
||||
"%timeit add_element_check1(rand_ints)\n",
|
||||
"%timeit add_element_check2(rand_ints)\n",
|
||||
"%timeit add_element_except(rand_ints)\n",
|
||||
"%timeit add_element_defaultdict(rand_ints)"
|
||||
"%timeit add_element_defaultdict(rand_ints)\n",
|
||||
"%timeit add_element_get(rand_ints)"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
|
@ -922,7 +932,7 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"Results for 100 integers in range 1-10\n",
|
||||
"10000 loops, best of 3: 24.6 \u00b5s per loop"
|
||||
"10000 loops, best of 3: 28 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -938,7 +948,7 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"10000 loops, best of 3: 25.4 \u00b5s per loop"
|
||||
"10000 loops, best of 3: 26.5 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -946,7 +956,15 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"10000 loops, best of 3: 23 \u00b5s per loop"
|
||||
"10000 loops, best of 3: 22.8 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"10000 loops, best of 3: 33.3 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -956,7 +974,7 @@
|
|||
"\n",
|
||||
"\n",
|
||||
"Results for 1000 integers in range 1-5\n",
|
||||
"1000 loops, best of 3: 236 \u00b5s per loop"
|
||||
"1000 loops, best of 3: 242 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -964,7 +982,7 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"1000 loops, best of 3: 235 \u00b5s per loop"
|
||||
"1000 loops, best of 3: 239 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -972,7 +990,7 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"1000 loops, best of 3: 207 \u00b5s per loop"
|
||||
"1000 loops, best of 3: 203 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -980,7 +998,15 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"10000 loops, best of 3: 177 \u00b5s per loop"
|
||||
"10000 loops, best of 3: 184 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"1000 loops, best of 3: 350 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -990,7 +1016,7 @@
|
|||
"\n",
|
||||
"\n",
|
||||
"Results for 1000 integers in range 1-1000\n",
|
||||
"1000 loops, best of 3: 268 \u00b5s per loop"
|
||||
"1000 loops, best of 3: 262 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -998,7 +1024,7 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"1000 loops, best of 3: 377 \u00b5s per loop"
|
||||
"1000 loops, best of 3: 370 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -1006,7 +1032,7 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"1000 loops, best of 3: 511 \u00b5s per loop"
|
||||
"1000 loops, best of 3: 502 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -1014,7 +1040,15 @@
|
|||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"1000 loops, best of 3: 410 \u00b5s per loop"
|
||||
"1000 loops, best of 3: 422 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"1000 loops, best of 3: 373 \u00b5s per loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -1025,7 +1059,32 @@
|
|||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 16
|
||||
"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",
|
||||
|
|
Loading…
Reference in New Issue
Block a user