Skip to content

Commit bd4c0fd

Browse files
operation on dictionary
1 parent 27305c6 commit bd4c0fd

File tree

1 file changed

+281
-0
lines changed

1 file changed

+281
-0
lines changed
Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"{'a': 45, 'b': 66, 'c': 44}\n"
13+
]
14+
}
15+
],
16+
"source": [
17+
"d={'a':45,'b':66,'c':44}\n",
18+
"print(d)"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 5,
24+
"metadata": {},
25+
"outputs": [
26+
{
27+
"data": {
28+
"text/plain": [
29+
"dict_keys(['a', 'b', 'c'])"
30+
]
31+
},
32+
"execution_count": 5,
33+
"metadata": {},
34+
"output_type": "execute_result"
35+
}
36+
],
37+
"source": [
38+
"d.keys()"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": 3,
44+
"metadata": {},
45+
"outputs": [
46+
{
47+
"data": {
48+
"text/plain": [
49+
"dict_values([45, 66, 44])"
50+
]
51+
},
52+
"execution_count": 3,
53+
"metadata": {},
54+
"output_type": "execute_result"
55+
}
56+
],
57+
"source": [
58+
"d.values()"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"## 2"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 11,
71+
"metadata": {},
72+
"outputs": [
73+
{
74+
"data": {
75+
"text/plain": [
76+
"{'a': 45, 'b': 44, 'c': 44}"
77+
]
78+
},
79+
"execution_count": 11,
80+
"metadata": {},
81+
"output_type": "execute_result"
82+
}
83+
],
84+
"source": [
85+
"d={'a':45,'b':66,'c':44,'b':44}\n",
86+
"d"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 12,
92+
"metadata": {},
93+
"outputs": [
94+
{
95+
"data": {
96+
"text/plain": [
97+
"True"
98+
]
99+
},
100+
"execution_count": 12,
101+
"metadata": {},
102+
"output_type": "execute_result"
103+
}
104+
],
105+
"source": [
106+
"'b' in d"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": 13,
112+
"metadata": {},
113+
"outputs": [
114+
{
115+
"data": {
116+
"text/plain": [
117+
"{'a': 45, 'b': 44, 'c': 123}"
118+
]
119+
},
120+
"execution_count": 13,
121+
"metadata": {},
122+
"output_type": "execute_result"
123+
}
124+
],
125+
"source": [
126+
"d['c']=123\n",
127+
"d"
128+
]
129+
},
130+
{
131+
"cell_type": "code",
132+
"execution_count": 14,
133+
"metadata": {},
134+
"outputs": [
135+
{
136+
"data": {
137+
"text/plain": [
138+
"{'a': 45, 'c': 123}"
139+
]
140+
},
141+
"execution_count": 14,
142+
"metadata": {},
143+
"output_type": "execute_result"
144+
}
145+
],
146+
"source": [
147+
"del(d['b'])\n",
148+
"d"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": 3,
154+
"metadata": {},
155+
"outputs": [
156+
{
157+
"name": "stdout",
158+
"output_type": "stream",
159+
"text": [
160+
"{'capital': 'paris', 'population': 66.03}\n",
161+
"{'spain': {'capital': 'madrid', 'population': 46.77}, 'france': {'capital': 'paris', 'population': 66.03}, 'germany': {'capital': 'berlin', 'population': 80.62}, 'norway': {'capital': 'oslo', 'population': 5.084}, 'italy': {'capital': 'rome', 'population': 59.83}}\n"
162+
]
163+
}
164+
],
165+
"source": [
166+
"# Dictionary of dictionaries\n",
167+
"europe = { 'spain': { 'capital':'madrid', 'population':46.77 },\n",
168+
" 'france': { 'capital':'paris', 'population':66.03 },\n",
169+
" 'germany': { 'capital':'berlin', 'population':80.62 },\n",
170+
" 'norway': { 'capital':'oslo', 'population':5.084 } }\n",
171+
"# Print out the capital of France\n",
172+
"print(europe['france'])\n",
173+
"# Create sub-dictionary data\n",
174+
"data={'capital':'rome','population':59.83}\n",
175+
"# Add data to europe under key 'italy'\n",
176+
"europe['italy']=data\n",
177+
"# Print europe\n",
178+
"print(europe)"
179+
]
180+
},
181+
{
182+
"cell_type": "code",
183+
"execution_count": 4,
184+
"metadata": {},
185+
"outputs": [
186+
{
187+
"data": {
188+
"text/html": [
189+
"<div>\n",
190+
"<style scoped>\n",
191+
" .dataframe tbody tr th:only-of-type {\n",
192+
" vertical-align: middle;\n",
193+
" }\n",
194+
"\n",
195+
" .dataframe tbody tr th {\n",
196+
" vertical-align: top;\n",
197+
" }\n",
198+
"\n",
199+
" .dataframe thead th {\n",
200+
" text-align: right;\n",
201+
" }\n",
202+
"</style>\n",
203+
"<table border=\"1\" class=\"dataframe\">\n",
204+
" <thead>\n",
205+
" <tr style=\"text-align: right;\">\n",
206+
" <th></th>\n",
207+
" <th>spain</th>\n",
208+
" <th>france</th>\n",
209+
" <th>germany</th>\n",
210+
" <th>norway</th>\n",
211+
" <th>italy</th>\n",
212+
" </tr>\n",
213+
" </thead>\n",
214+
" <tbody>\n",
215+
" <tr>\n",
216+
" <th>capital</th>\n",
217+
" <td>madrid</td>\n",
218+
" <td>paris</td>\n",
219+
" <td>berlin</td>\n",
220+
" <td>oslo</td>\n",
221+
" <td>rome</td>\n",
222+
" </tr>\n",
223+
" <tr>\n",
224+
" <th>population</th>\n",
225+
" <td>46.77</td>\n",
226+
" <td>66.03</td>\n",
227+
" <td>80.62</td>\n",
228+
" <td>5.084</td>\n",
229+
" <td>59.83</td>\n",
230+
" </tr>\n",
231+
" </tbody>\n",
232+
"</table>\n",
233+
"</div>"
234+
],
235+
"text/plain": [
236+
" spain france germany norway italy\n",
237+
"capital madrid paris berlin oslo rome\n",
238+
"population 46.77 66.03 80.62 5.084 59.83"
239+
]
240+
},
241+
"execution_count": 4,
242+
"metadata": {},
243+
"output_type": "execute_result"
244+
}
245+
],
246+
"source": [
247+
"import pandas as pd\n",
248+
"d=pd.DataFrame(europe)\n",
249+
"d"
250+
]
251+
},
252+
{
253+
"cell_type": "code",
254+
"execution_count": null,
255+
"metadata": {},
256+
"outputs": [],
257+
"source": []
258+
}
259+
],
260+
"metadata": {
261+
"kernelspec": {
262+
"display_name": "Python 3",
263+
"language": "python",
264+
"name": "python3"
265+
},
266+
"language_info": {
267+
"codemirror_mode": {
268+
"name": "ipython",
269+
"version": 3
270+
},
271+
"file_extension": ".py",
272+
"mimetype": "text/x-python",
273+
"name": "python",
274+
"nbconvert_exporter": "python",
275+
"pygments_lexer": "ipython3",
276+
"version": "3.7.5"
277+
}
278+
},
279+
"nbformat": 4,
280+
"nbformat_minor": 2
281+
}

0 commit comments

Comments
 (0)