Skip to content

Instantly share code, notes, and snippets.

@fakechris
Created April 7, 2014 09:01
Show Gist options
  • Save fakechris/10016973 to your computer and use it in GitHub Desktop.
Save fakechris/10016973 to your computer and use it in GitHub Desktop.
Python Tutorials dict and list
{
"metadata": {
"name": "Python Tutorials 2"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": "type([])",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 1,
"text": "list"
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": "a = [1,2,\"a\"]",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": "[i for i in a]",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": "[1, 2, 'a']"
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": "type(())",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": "tuple"
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": "b = (1,2,\"x\")",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": "a.append(\"c\")",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": "a",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 10,
"text": "[1, 2, 'a', 'c']"
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": "b.append(\"c\")",
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'tuple' object has no attribute 'append'",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-11-77626248ca13>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mb\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"c\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": "dir(list)",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 15,
"text": "['__add__',\n '__class__',\n '__contains__',\n '__delattr__',\n '__delitem__',\n '__delslice__',\n '__doc__',\n '__eq__',\n '__format__',\n '__ge__',\n '__getattribute__',\n '__getitem__',\n '__getslice__',\n '__gt__',\n '__hash__',\n '__iadd__',\n '__imul__',\n '__init__',\n '__iter__',\n '__le__',\n '__len__',\n '__lt__',\n '__mul__',\n '__ne__',\n '__new__',\n '__reduce__',\n '__reduce_ex__',\n '__repr__',\n '__reversed__',\n '__rmul__',\n '__setattr__',\n '__setitem__',\n '__setslice__',\n '__sizeof__',\n '__str__',\n '__subclasshook__',\n 'append',\n 'count',\n 'extend',\n 'index',\n 'insert',\n 'pop',\n 'remove',\n 'reverse',\n 'sort']"
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": "dir(tuple)",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 14,
"text": "['__add__',\n '__class__',\n '__contains__',\n '__delattr__',\n '__doc__',\n '__eq__',\n '__format__',\n '__ge__',\n '__getattribute__',\n '__getitem__',\n '__getnewargs__',\n '__getslice__',\n '__gt__',\n '__hash__',\n '__init__',\n '__iter__',\n '__le__',\n '__len__',\n '__lt__',\n '__mul__',\n '__ne__',\n '__new__',\n '__reduce__',\n '__reduce_ex__',\n '__repr__',\n '__rmul__',\n '__setattr__',\n '__sizeof__',\n '__str__',\n '__subclasshook__',\n 'count',\n 'index']"
}
],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": "type(set())",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 17,
"text": "set"
}
],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": "c = set(['a', 'c', 12])",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": "dir(set)",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 20,
"text": "['__and__',\n '__class__',\n '__cmp__',\n '__contains__',\n '__delattr__',\n '__doc__',\n '__eq__',\n '__format__',\n '__ge__',\n '__getattribute__',\n '__gt__',\n '__hash__',\n '__iand__',\n '__init__',\n '__ior__',\n '__isub__',\n '__iter__',\n '__ixor__',\n '__le__',\n '__len__',\n '__lt__',\n '__ne__',\n '__new__',\n '__or__',\n '__rand__',\n '__reduce__',\n '__reduce_ex__',\n '__repr__',\n '__ror__',\n '__rsub__',\n '__rxor__',\n '__setattr__',\n '__sizeof__',\n '__str__',\n '__sub__',\n '__subclasshook__',\n '__xor__',\n 'add',\n 'clear',\n 'copy',\n 'difference',\n 'difference_update',\n 'discard',\n 'intersection',\n 'intersection_update',\n 'isdisjoint',\n 'issubset',\n 'issuperset',\n 'pop',\n 'remove',\n 'symmetric_difference',\n 'symmetric_difference_update',\n 'union',\n 'update']"
}
],
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": "range(1, 10, 2)",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 21,
"text": "[1, 3, 5, 7, 9]"
}
],
"prompt_number": 21
},
{
"cell_type": "code",
"collapsed": false,
"input": "enumerate(range(1, 10))",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 22,
"text": "<enumerate at 0x101c61fa0>"
}
],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": "list(enumerate(range(1, 10)))",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 23,
"text": "[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9)]"
}
],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": "[x for x in enumerate(range(1, 10))]",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 24,
"text": "[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9)]"
}
],
"prompt_number": 24
},
{
"cell_type": "code",
"collapsed": false,
"input": "def rangei(fromi, toi):\n i = fromi\n while i < toi:\n yield i\n i+=1",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 29
},
{
"cell_type": "code",
"collapsed": false,
"input": "[x for x in rangei(1,10)]",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 30,
"text": "[1, 2, 3, 4, 5, 6, 7, 8, 9]"
}
],
"prompt_number": 30
},
{
"cell_type": "code",
"collapsed": false,
"input": "type({})",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 32,
"text": "dict"
}
],
"prompt_number": 32
},
{
"cell_type": "code",
"collapsed": false,
"input": "d = {'a':1, 2:\"b\"}",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 33
},
{
"cell_type": "code",
"collapsed": false,
"input": "dir(dict)",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 34,
"text": "['__class__',\n '__cmp__',\n '__contains__',\n '__delattr__',\n '__delitem__',\n '__doc__',\n '__eq__',\n '__format__',\n '__ge__',\n '__getattribute__',\n '__getitem__',\n '__gt__',\n '__hash__',\n '__init__',\n '__iter__',\n '__le__',\n '__len__',\n '__lt__',\n '__ne__',\n '__new__',\n '__reduce__',\n '__reduce_ex__',\n '__repr__',\n '__setattr__',\n '__setitem__',\n '__sizeof__',\n '__str__',\n '__subclasshook__',\n 'clear',\n 'copy',\n 'fromkeys',\n 'get',\n 'has_key',\n 'items',\n 'iteritems',\n 'iterkeys',\n 'itervalues',\n 'keys',\n 'pop',\n 'popitem',\n 'setdefault',\n 'update',\n 'values',\n 'viewitems',\n 'viewkeys',\n 'viewvalues']"
}
],
"prompt_number": 34
},
{
"cell_type": "code",
"collapsed": false,
"input": "d.items()",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 35,
"text": "[('a', 1), (2, 'b')]"
}
],
"prompt_number": 35
},
{
"cell_type": "code",
"collapsed": false,
"input": "[(key,value) for key,value in d.items()]",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 36,
"text": "[('a', 1), (2, 'b')]"
}
],
"prompt_number": 36
},
{
"cell_type": "code",
"collapsed": false,
"input": "from collections import defaultdict",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 38
},
{
"cell_type": "code",
"collapsed": false,
"input": "d1 = defaultdict(list)",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 40
},
{
"cell_type": "code",
"collapsed": false,
"input": "d1.setdefault('key', []).append(12)",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 41
},
{
"cell_type": "code",
"collapsed": false,
"input": "d1",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 42,
"text": "defaultdict(<type 'list'>, {'key': [12]})"
}
],
"prompt_number": 42
},
{
"cell_type": "code",
"collapsed": false,
"input": "d1.setdefault('key', []).append(24)",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 43
},
{
"cell_type": "code",
"collapsed": false,
"input": "d1",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 44,
"text": "defaultdict(<type 'list'>, {'key': [12, 24]})"
}
],
"prompt_number": 44
},
{
"cell_type": "code",
"collapsed": false,
"input": "d = {}\nkey = \"key\"\nv = 12\nif d.has_key(key):\n d[key].append(v)\nelse:\n d[key]=[v]",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 46
},
{
"cell_type": "code",
"collapsed": false,
"input": "d",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 47,
"text": "{'key': [12]}"
}
],
"prompt_number": 47
},
{
"cell_type": "code",
"collapsed": false,
"input": "from collections import namedtuple",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 49
},
{
"cell_type": "code",
"collapsed": false,
"input": "point = namedtuple('Point', 'x y z')",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 51
},
{
"cell_type": "code",
"collapsed": false,
"input": "point(0,0,0)",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 52,
"text": "Point(x=0, y=0, z=0)"
}
],
"prompt_number": 52
},
{
"cell_type": "code",
"collapsed": false,
"input": "type(point)",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 53,
"text": "type"
}
],
"prompt_number": 53
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment