Skip to content

Commit 7132050

Browse files
committed
u11
1 parent ca02ce6 commit 7132050

6 files changed

+592
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,234 @@
11
{
2-
"cells": [],
3-
"metadata": {},
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#Advanced Dictionaries\n",
8+
"Unlike some of the other Data Structures we've worked with, most of the really useful methods available to us in Dictionaries have already been explored throughout this course. Here we will touch on just a few more for good measure:"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 17,
14+
"metadata": {
15+
"collapsed": true
16+
},
17+
"outputs": [],
18+
"source": [
19+
"d = {'k1':1,'k2':2}"
20+
]
21+
},
22+
{
23+
"cell_type": "markdown",
24+
"metadata": {},
25+
"source": [
26+
"##Dictionary Comprehensions\n",
27+
"\n",
28+
"Just like List Comprehensions, Dictionary Data Types also support their own version of comprehension for quick creation. It is not as commonly used as List Comprehensions, but the syntax is:"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 12,
34+
"metadata": {
35+
"collapsed": false
36+
},
37+
"outputs": [
38+
{
39+
"data": {
40+
"text/plain": [
41+
"{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}"
42+
]
43+
},
44+
"execution_count": 12,
45+
"metadata": {},
46+
"output_type": "execute_result"
47+
}
48+
],
49+
"source": [
50+
"{x:x**2 for x in range(10)}"
51+
]
52+
},
53+
{
54+
"cell_type": "markdown",
55+
"metadata": {},
56+
"source": [
57+
"One of the reasons it is not as common is the difficulty in structuring the key names that are not based off the values."
58+
]
59+
},
60+
{
61+
"cell_type": "markdown",
62+
"metadata": {},
63+
"source": [
64+
"##Iteration over keys,values, and items\n",
65+
"Dictionaries can be iterated over using the iter methods available in a dictionary. For example:"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 14,
71+
"metadata": {
72+
"collapsed": false
73+
},
74+
"outputs": [
75+
{
76+
"name": "stdout",
77+
"output_type": "stream",
78+
"text": [
79+
"k2\n",
80+
"k1\n"
81+
]
82+
}
83+
],
84+
"source": [
85+
"for k in d.iterkeys():\n",
86+
" print k"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 15,
92+
"metadata": {
93+
"collapsed": false
94+
},
95+
"outputs": [
96+
{
97+
"name": "stdout",
98+
"output_type": "stream",
99+
"text": [
100+
"2\n",
101+
"1\n"
102+
]
103+
}
104+
],
105+
"source": [
106+
"for v in d.itervalues():\n",
107+
" print v"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": 16,
113+
"metadata": {
114+
"collapsed": false
115+
},
116+
"outputs": [
117+
{
118+
"name": "stdout",
119+
"output_type": "stream",
120+
"text": [
121+
"('k2', 2)\n",
122+
"('k1', 1)\n"
123+
]
124+
}
125+
],
126+
"source": [
127+
"for item in d.iteritems():\n",
128+
" print item"
129+
]
130+
},
131+
{
132+
"cell_type": "markdown",
133+
"metadata": {},
134+
"source": [
135+
"#view items,keys and values\n",
136+
"You can use the view methods to view items keys and values. For example:"
137+
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": 2,
142+
"metadata": {
143+
"collapsed": false
144+
},
145+
"outputs": [
146+
{
147+
"data": {
148+
"text/plain": [
149+
"dict_items([('k2', 2), ('k1', 1)])"
150+
]
151+
},
152+
"execution_count": 2,
153+
"metadata": {},
154+
"output_type": "execute_result"
155+
}
156+
],
157+
"source": [
158+
"d.viewitems()"
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": 5,
164+
"metadata": {
165+
"collapsed": false
166+
},
167+
"outputs": [
168+
{
169+
"data": {
170+
"text/plain": [
171+
"dict_keys(['k2', 'k1'])"
172+
]
173+
},
174+
"execution_count": 5,
175+
"metadata": {},
176+
"output_type": "execute_result"
177+
}
178+
],
179+
"source": [
180+
"d.viewkeys()"
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": 18,
186+
"metadata": {
187+
"collapsed": false
188+
},
189+
"outputs": [
190+
{
191+
"data": {
192+
"text/plain": [
193+
"dict_values([2, 1])"
194+
]
195+
},
196+
"execution_count": 18,
197+
"metadata": {},
198+
"output_type": "execute_result"
199+
}
200+
],
201+
"source": [
202+
"d.viewvalues()"
203+
]
204+
},
205+
{
206+
"cell_type": "markdown",
207+
"metadata": {},
208+
"source": [
209+
"**Great! You should now feel very comfortable using the variety of methods available to you in Dictionaries!**"
210+
]
211+
}
212+
],
213+
"metadata": {
214+
"kernelspec": {
215+
"display_name": "Python 2",
216+
"language": "python",
217+
"name": "python2"
218+
},
219+
"language_info": {
220+
"codemirror_mode": {
221+
"name": "ipython",
222+
"version": 2
223+
},
224+
"file_extension": ".py",
225+
"mimetype": "text/x-python",
226+
"name": "python",
227+
"nbconvert_exporter": "python",
228+
"pygments_lexer": "ipython2",
229+
"version": "2.7.10"
230+
}
231+
},
4232
"nbformat": 4,
5233
"nbformat_minor": 0
6234
}

.ipynb_checkpoints/Advanced Tuples-checkpoint.ipynb

-6
This file was deleted.
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {
7+
"collapsed": true
8+
},
9+
"outputs": [],
10+
"source": [
11+
"t = (1,2,3)"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 3,
17+
"metadata": {
18+
"collapsed": false
19+
},
20+
"outputs": [
21+
{
22+
"ename": "TypeError",
23+
"evalue": "count() takes exactly one argument (0 given)",
24+
"output_type": "error",
25+
"traceback": [
26+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
27+
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
28+
"\u001b[0;32m<ipython-input-3-3e3482324d98>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcount\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
29+
"\u001b[0;31mTypeError\u001b[0m: count() takes exactly one argument (0 given)"
30+
]
31+
}
32+
],
33+
"source": []
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"metadata": {
39+
"collapsed": true
40+
},
41+
"outputs": [],
42+
"source": []
43+
}
44+
],
45+
"metadata": {
46+
"kernelspec": {
47+
"display_name": "Python 2",
48+
"language": "python",
49+
"name": "python2"
50+
},
51+
"language_info": {
52+
"codemirror_mode": {
53+
"name": "ipython",
54+
"version": 2
55+
},
56+
"file_extension": ".py",
57+
"mimetype": "text/x-python",
58+
"name": "python",
59+
"nbconvert_exporter": "python",
60+
"pygments_lexer": "ipython2",
61+
"version": "2.7.10"
62+
}
63+
},
64+
"nbformat": 4,
65+
"nbformat_minor": 0
66+
}

0 commit comments

Comments
 (0)