40.Example programs using Dictionaries
Program to count the number of occurrence(frequency) of each letters in a given string( histogram) ( university question)
S=input("Enter the string…..")d=dict()
for c in S:
d[c]=d.get(c,0)+1
print("letter count")
print(d)
Program to display the frequency of each word in a given string.(university qstn)
S=input("Enter the string…..")
S=S.split(" ") #splitting it into words
d=dict()
for w in S:
d[w]=d.get(w,0)+1
print("word count")
print(d)
Write a Python program to create a dictionary of roll numbers and names of five students. Display the names in the dictionary in alphabetical order.(university question)
d={}
for i in range(5):
rn=int(input("Enter roll number.."))
name=input("Enter name …")
d[rn]=name
l=list(d.items())
l.sort(key=lambda v:v[1])
print("name and roll number in sorted order of name")
for i in l:
print(i[1],":",i[0])
Program to read name and phn numbers of ‘n’ customers and print the list in sorted order of names
n=int(input("Enter number of customers.."))
d={}
for i in range(n):
nm=input("Enter name..")
phn=int(input("Enter phn number…"))
d[nm]=phn
l=list(d.items()) # creating a list
l.sort() # sorting the list in the order of name..
#l.sort(key=lambda v:v[1]) will sort in the order of phone number
print("name and phn number in sorted order")
for i in l:
print(i[0],":",i[1])
In the above program after printing the sorted list, read a name and delete that customer from the dictionary if present.
n=int(input("Enter number of customers.."))
d={}
for i in range(n):
nm=input("Enter name..")
phn=int(input("Enter phn number…"))
d[nm]=phn
l=list(d.items()) # creating a list
l.sort() # sorting the list in the order of name..
#l.sort(key=lambda v:v[1]) will sort in the order of phone number
print("name and phn number in sorted order")
for i in l:
print(i[0],":",i[1])
k=input("Enter a name to search..")
if k not in d:
print("Not found the name")
else:
print("Details..",k,":",d[k])
del(d[k]) # deleting the entry
print("Dictionary after deletion")
print(d)
The following program uses a dictionary to convert hexadecimal number into binary.
hextobin={'0':'0000','1':'0001','2':'0010','3':'0011','4':'0100','5':'0101','6':'0110','7':'0111','8':'1000','9':'1001','A':'1010','B':'1011','C':'1100','D':'1101','E':'1110','F':'1111'}
n=input('Enter the hexadecimal number….')
bn=''
n=n.upper()
for d in n:
h=hextobin.get(d)
if h==None:
print('Invalid Number')
break
bn=bn+hextobin[d]
else:
print('Binary equivalent is..',bn)
Finding the mode of list of numbers
n=int(input("Enter..how many numebrs.."))
print("Enter {} numbers".format(n))
numbers=[]
for i in range(n):
x=int(input())
numbers.append(x)
ncount={}
for x in numbers:
ncount[x]=ncount.get(x,0)+1
maxcount=max(ncount.values())
print('Mode..')
for k in ncount:
if ncount[k]==maxcount:
print(k)
A matrix which contains large number of zeros is called sparse matrix. A better way to represent sparse matrix is my storing only the non zero element and its index using a dictionary.
m=int(input("Enter the number of rows....."))
n=int(input("Enter the number of columns....."))
print("Enter the elements of the sparse matrix ...")
A=[[0 for c in range(n)]for r in range(m)]
for r in range(m):
for c in range(n):
A[r][c]=int(input())
D={}
for r in range(m):
for c in range(n):
if A[r][c]!=0: #storing the non zero elements in the dictionary
D[r,c]=A[r][c]
print("The Sparse Matrix is.....")
for i in A:
print (i)
print("The Dictionary Representation is....." )
print(D)
Write a Python code to create a function called list_of_frequency that takes a string and prints the letters in non-increasing order of the frequency of their occurrences. Use dictionaries.
def list_of_frequency(s):
d=dict()
for c in s:
d[c]=d.get(c,0)+1
print("letter count in the decresing order")
l=list(d.items())
l.sort(key=lambda x:x[1],reverse=True)
print(l)
s=input("Enter the string…..")
list_of_frequency(s)
Write a Python program to combine two dictionary adding values for common keys. d1 = {'a':10, 'b': 20, 'c':30} d2 = {'a': 30, 'b': 20, 'd':40} Sample output: {'a': 40, 'b': 40, 'd': 40, 'c': 30}
( university question)
d1 = {'a': 10, 'b': 20, 'c': 30}
d2 = {'a': 30, 'b': 20, 'd': 40}
#updating d1 with modified values
for key in d1:
d1[key]=d2.get(key,0)+d1[key]
for key in d2:
if key not in d1:
d1[key]=d2[key]
print(d1)
output
{'a': 40, 'b': 40, 'c': 30, 'd': 40}
Write a Python script to print a dictionary where the keys are numbers between 1 and 15 (both included) and the values are square of keys.( university question)
d=dict()for x in range(1,16):
d[x]=x**2
print(d)
Comments
Post a Comment