Skip to content

Commit 68eb7eb

Browse files
committed
added
1 parent 410aded commit 68eb7eb

File tree

72 files changed

+23234
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+23234
-0
lines changed

Advanced Dictionaries.ipynb

+234
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
{
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+
},
232+
"nbformat": 4,
233+
"nbformat_minor": 0
234+
}

0 commit comments

Comments
 (0)