Python Program Practice 3
Python Program Practice 3
In [4]:
# create a dictionary
dict1 = {'data': '123',
'science':'234',
'AI': '235'}
In [5]:
#solution
for key in sorted(dict1):
print("%s: %s" % (key, dict1[key]))
AI: 235
data: 123
science: 234
In [6]:
In [7]:
#solution
class dictObj(object):
def __init__(self):
self.x = 'algorithm'
self.y = 'statistics'
self.z = 'programming'
def do_nothing(self):
pass
test = dictObj()
print(test.__dict__)
In [8]:
http://localhost:8888/nbconvert/html/Desktop/IPYNB_Files/10th%20April_Let's%20do%20Together/Let's%20Do%20Together%20-%20Session%203.ipynb?down
5/3/2018 Let's Do Together - Session 3
In [9]:
#solution
student_data = {'id1':
{'name': ['Sara'],
'class': ['V'],
'subject_integration': ['english, math, science']
},
'id2':
{'name': ['David'],
'class': ['V'],
'subject_integration': ['english, math, science']
},
'id3':
{'name': ['Sara'],
'class': ['V'],
'subject_integration': ['english, math, science']
},
'id4':
{'name': ['Surya'],
'class': ['V'],
'subject_integration': ['english, math, science']
},
}
result = {}
print(result)
In [10]:
# write a program to combine two dictionary adding values for common keys
In [11]:
#solution
from collections import Counter
d1 = {'a': 1000, 'b': 3200, 'c':1300}
d2 = {'a': 300, 'b': 200, 'd':400}
d = Counter(d1) + Counter(d2)
print(d)
In [26]:
http://localhost:8888/nbconvert/html/Desktop/IPYNB_Files/10th%20April_Let's%20do%20Together/Let's%20Do%20Together%20-%20Session%203.ipynb?down
5/3/2018 Let's Do Together - Session 3
In [27]:
In [28]:
#solution
u_value = set( val for dic in data for val in dic.values())
print("Unique Values: ",u_value)
In [29]:
type(data)
Out[29]:
list
In [ ]:
http://localhost:8888/nbconvert/html/Desktop/IPYNB_Files/10th%20April_Let's%20do%20Together/Let's%20Do%20Together%20-%20Session%203.ipynb?down