0% found this document useful (0 votes)
13 views

Python Program Practice 3

Uploaded by

yusuf01asif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Python Program Practice 3

Uploaded by

yusuf01asif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

5/3/2018 Let's Do Together - Session 3

In [4]:

# create a dictionary
dict1 = {'data': '123',
'science':'234',
'AI': '235'}

# write a program to sort a dictionary by key.

In [5]:

#solution
for key in sorted(dict1):
print("%s: %s" % (key, dict1[key]))

AI: 235
data: 123
science: 234

In [6]:

#Write a Python program to get a dictionary from an object's fields.

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__)

{'x': 'algorithm', 'y': 'statistics', 'z': 'programming'}

In [8]:

# write a program to remove duplicates from Dictionary.

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 = {}

for key,value in student_data.items():


if value not in result.values():
result[key] = value

print(result)

{'id1': {'name': ['Sara'], 'class': ['V'], 'subject_integration': ['englis


h, math, science']}, 'id2': {'name': ['David'], 'class': ['V'], 'subject_i
ntegration': ['english, math, science']}, 'id4': {'name': ['Surya'], 'clas
s': ['V'], 'subject_integration': ['english, math, science']}}

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)

Counter({'b': 3400, 'a': 1300, 'c': 1300, 'd': 400})

In [26]:

# write a program to print all unique values from a dictionary in a list

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]:

data = [{"V":"S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"}, {"VII":"S005"}, {


"V":"S009"},{"VIII":"S007"}]

In [28]:

#solution
u_value = set( val for dic in data for val in dic.values())
print("Unique Values: ",u_value)

Unique Values: {'S007', 'S002', 'S001', 'S009', 'S005'}

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

You might also like