mirror of
https://github.com/donnemartin/data-science-ipython-notebooks.git
synced 2024-03-22 13:30:56 +08:00
Added dict items() snippet. Tweaked a few snippets and comments.
This commit is contained in:
parent
5131bc8a46
commit
c8a06dcd85
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"metadata": {
|
||||
"name": "",
|
||||
"signature": "sha256:6d8f33055bf348eb4c2871340e68a3f7577e0a027f5c83423e35757e01e8243a"
|
||||
"signature": "sha256:af3d280883daa542ff4a3cccb240979e6b7c05fbe40cac48675cc8ef613e3483"
|
||||
},
|
||||
"nbformat": 3,
|
||||
"nbformat_minor": 0,
|
||||
|
@ -902,7 +902,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"dict keys must be \"hashable\": immutable objects like scalars (int, float, string) or tuples whose objects are all immutable."
|
||||
"dict keys must be \"hashable\": immutable objects like scalars (int, float, string) or tuples whose objects are all immutable. Lists are mutable and therefore are not hashable, although you can convert the list portion to a tuple as a quick fix"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -926,35 +926,6 @@
|
|||
],
|
||||
"prompt_number": 32
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Lists are mutable and therefore are not hashable, although you can convert the list portion to a tuple as a quick fix"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"hash((1, 2, [3, 4]))"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "TypeError",
|
||||
"evalue": "unhashable type: 'list'",
|
||||
"output_type": "pyerr",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m<ipython-input-33-94f25bbf31b2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mhash\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||||
"\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 33
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
|
@ -974,13 +945,13 @@
|
|||
{
|
||||
"metadata": {},
|
||||
"output_type": "pyout",
|
||||
"prompt_number": 34,
|
||||
"prompt_number": 33,
|
||||
"text": [
|
||||
"['a', 'b', 5, 'z']"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 34
|
||||
"prompt_number": 33
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
|
@ -1001,12 +972,42 @@
|
|||
{
|
||||
"metadata": {},
|
||||
"output_type": "pyout",
|
||||
"prompt_number": 35,
|
||||
"prompt_number": 34,
|
||||
"text": [
|
||||
"['foo', [0, 1, 2, 3], 'bar', None]"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 34
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Iterate through a dictionary's keys and values"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"for key, value in dict_1.items():\n",
|
||||
" print key, value"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"a foo\n",
|
||||
"b [0, 1, 2, 3]\n",
|
||||
"5 bar\n",
|
||||
"z None\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 35
|
||||
},
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue
Block a user