Multidimensional DICTIONARY?
Using this we can create dictionary keys to contain list, tuple, set, and dictionary objects to represent multiple data. It is used to manage the record of multiple entities using single objects.
Syntax of Multidimensional Dictionary:-
var = {'key': list | set | dict,...}
s = {'A':[101,102,103],'B':[34,56,78]}
Now I will contain multiple employee records and display them.
Solution:-
emp = {"empid":[1001,1002,1003],"empname":["xyz","abc","mno"],"job":["mgr","cleark"]}
for key in emp:
print(key)
for val in emp[key]:
print(val,end=' ')
print()
Dictionary can contain List, Tuple, Set, and Dictionary type Objects?
1) Dictionary to List
emp = {"empid":[1001,1002,1003],"empname":["xyz","abc","mno"],"job":["mgr","cleark"]}
Display elements:-
for key in emp:
print(key)
for val in emp[key]:
print(val,end=' ')
print()
2) Dictionary to Tuple
emp = {"empid":(1001,1002,1003),"empname":("xyz","abc","mno"),"job":("mgr","cleark")}
Display elements:-
for key in emp:
print(key)
for val in emp[key]:
print(val,end=' ')
print()
3) Dictionary to Set
emp = {"empid":{1001,1002,1003},"empname":{"xyz","abc","mno"},"job":{"mgr","cleark"}}
Display elements:-
for key in emp:
print(key)
for val in emp[key]:
print(val,end=' ')
print()
4) Dictionary to Dictionary
emp = {"branch1":{"empid":1001,"empname":"Jay Kumar"},"branch2":{"empid":1002,"empname":"Manish Kumar"}}
Display elements:-
for key in emp:
print(key)
for val in emp[key]:
print(val,emp[key][val],end=' ')
print()
Another Example of Dictionary to Dictionary and List:
student = {"A":{"rno":[1001,1003,1005],"sname":["Manish","Jay","Ravi"]},"B":{"rno":[2001,2003,2005],"sname":["Sachin","Vijay","Kavi"]}}
print(student["A"]["rno"][2])
for s in student:
print(s,end=' ')
for d in student[s]:
for e in student[s][d]:
print(e,end=' ')
print()
5) Dictionary to all:-
we can store any type of objects under a dictionary key
emp = {"branch1":{"empid":1001,"empname":"Jay Kumar"},"branch2":[1002,"Manish Kumar"]}
for key in emp:
print(key)
for val in emp[key]:
if(type(emp[key]).__name__=='dict'):
print(val,emp[key][val],end=' ')
else:
print(val,end='')
print()
ASSIGNMENT OF DICTIONARY?
create a multi-dimension dictionary to store five student records and display max, min, total fees of students.
rno name branch fees
1001 xyz CS 45000
1002 abc IT 22000
The solution of This Program:-
stu = {'rno':[1001,1002],'sname':['manish','ramesh'],'branch':['CS','IT'],'fees':[45000,12000]}
for key in stu:
print(key,end=' ')
print()
for j in range(0,len(stu['rno'])):
print(str(stu['rno'][j])+" "+stu['sname'][j]+" "+stu['branch'][j]+" "+str(stu['fees'][j]))
Assignment of Dictionary Objects:-
WAP to convert a dictionary to set and set to the dictionary.
WAP to convert a dictionary to list and list to the dictionary.
WAP to create a multi-dimensions dictionary?
WAP to create a multi-dimension set?
Solution:-
stu = set({2,3,4,5,6,7}) # set to set, set to list, set to tuple, set to dict
for k in stu:
print(k)
WAP to convert Multi dimension Dictionary to Set?
The solution to this assignment:-
stu = {"a":{"a1":10,"a2":20},"b":{"b1":100,"b2":200}}
set1 = set()
set2 = set()
set3 = set()
for k in stu:
print(k)
set1.add(k)
for j in stu[k]:
print(j,stu[k][j])
set2.add(j)
set3.add(stu[k][j])
print(set1, set2, set3)
WAP to convert set to a tuple?
WAP to convert tuple to set?
WAP to convert set to list?
WAP to create Mark-sheet Program using Dictionary?
#convert dicitonary to list
ReplyDeletedict={'one':1,'two':2,'three':3,'four':4}
list1=[]
list2=[]
for i in dict:
list1.append(i)
list2.append(dict[i])
print(list1,list2)
#convert dicitonary to set
ReplyDeletedc={'one':1,'two':2,'three':3,'four':4}
s=set()
s1=set()
for i in dc:
s.add(i)
s1.add(dc[i])
print(s,s1)
#set to dictionary
ReplyDeletes={1,2,3,4,5,6,7}
d={k:'hi' for k in s}
print(d)
#list to dicitonary
ReplyDeletel=['one',12,'two',14,'three',14]
d={k:'a' for k in l}
print(d)
create two list one for key and another one for value
Delete#WAP to create a multi-dimensions dictionary?
ReplyDeletedata={'no':[1,2],'name':['a','b'],'post':[3,4],'salary':[20,40]}
for i in data:
print(i,end=' ')
print()
for j in range(0,len('no')):
print(str(data['no'][j])+' '+str(data['name'][j])+' '+str(data['post'][j])+' '+str(data['salary'][j]))
print()
#WAP to create a multi-dimension set?
ReplyDeletea = { {2, 4, 6, 8 }, {2, 3, 5, 7 }}
for i in range(len(a)) :
for j in range(len(a[i])) :
print(a[i][j], end=" ")
print()
doubt-error occur when run this program
a = { {2, 4, 6, 8 }, {2, 3, 5, 7 }}
TypeError: unhashable type: 'set'
>>>
#wap to convert set to a tuple
ReplyDeletes={'a',1,2,'b',40}
print("before conversion",s)
tup=tuple(s)
print("after conversion",tup)
#tuple to set
ReplyDeletetup=(1,2,3,'hi','hello')
a=type(tup)
print("before conversion",a,tup)
s=set(tup)
b=type(s)
print("after conversion",b,s)
#wap convert set to list
ReplyDeletes={'a',1,2,'c'}
l=[]
for i in s:
l.append(i)
print(l)
"""Q) WAP to Calculate Marksheet using five different subjects with the following condition.
ReplyDelete1) all subject marks should be 0 to 100.
3) if all subject marks are >=33 then percentage and division should be calculated.
4) if a student is suppl then five bonus marks can be applied to be pass and the result will be "pass by grace".
5) Display Grace Subject name, distinction subject name,suppl subject name, and failed subject name.
""""
dict={'phy':90,'che':40,'emg':89,'maths':29,'hindi':89}
count=0
gm=0
s=''
dist=''
for i in dict:
if dict[i]>=0 and dict[i]<=100:
print(dict[i])
if dict[i]<33:
count=count+1
s=s+i+ " "
gm=dict[i]
else:
if dict[i]>=75:
dist = dist+ i + " "
else:
print("All subject marks should be 0 to 100")
break
total=total+marks[i]
else:
if count==0 or(count==1 and gm>=29):
per= total/5
if per>33 and per<45:
print("Third division with {0} %".format(per))
elif per<60:
print("Second division with {0} %".format(per))
else:
print("First division with {0} %".format(per))
if gm>0:
print("You are pass by grace and grace marks is ",str(33-gm), "grace subject is ",s)
if dist!=0:
print("Distinction subject name is ",dist)
elif count==1:
print("suppl in ",s)
else:
print("fail in ",s)
#sir this program not run properly
#WAP to convert Multi dimension Dictionary to Set?
ReplyDeletes=set()
s1=set()
stu = {'rno':[1001,1002],'sname':['manish','ramesh'],'branch':['CS','IT'],'fees':[45000,12000]}
for key in stu:
s.add(key)
print(s)
for s1 in range(0,len(stu['rno'])):
print(str(stu['rno'][s1])+" "+stu['sname'][s1]+" "+stu['branch'][s1]+" "+str(stu['fees'][s1]))
#output of this program is
{'rno', 'fees', 'branch', 'sname'}
1001 manish CS 45000
1002 ramesh IT 12000
>>>not appropiate output come
Program to convert set to dictionary-->>
ReplyDeletes={"c","c++","vb",".net","php","python","java","testing"}
dic={y:"scs100" for y in s}
print(dic)
Program to convert dictionary to list-->>
ReplyDeletedic={'one':100,'two':99,'three':98,'four':97}
l1=[]
l2=[]
for i in dic:
l1.append(i)
l2.append(dic[i])
print(l1,l2)
Program to convert list to dictionary-->>
ReplyDeletel1=["a","b","c","d"]
l2=[324,361,400,441]
l=l1+l2
dic={k:"squares" for k in l}
print(dic)
DOUBT-->>Sir output should be like this-->>
{'a': 'squares', 'b': 'squares', 'c': 'squares', 'd': 'squares', 324: 'squares', 361: 'squares', 400: 'squares', 441: 'squares'}
Program to create multi dimension list-->>
ReplyDeletestu={'rollno.':[11,15],'sname':['suresh','ramesh'],'branch':['ec','mech'],'fees':[65000,66000]}
for key in stu:
print(key,end=' ')
print()
for j in range(0,len(stu['rollno.'])):
print(str(stu['rollno.'][j])+" "+stu['sname'][j]+" "+stu['branch'][j]+" "+str(stu['fees'][j]))
Program to create multi dimension set-->>
ReplyDeletestu=set((2,4,6,8,4,9))#set to set
for k in stu:
print(k)
Similarly
stu=set({2,4,6,8,4,9})#set to dictionary
stu=set([2,4,6,8,4,9])#set to list
stu=set((2,4,6,8,4,9))#set to tuple
Program to convert multi dimension dict to set-->>
ReplyDeletestu={"a":{"a1":101,"a2":201},"b":{"b1":999,"b2":998}}
set1=set()
set2=set()
set3=set()
for k in stu:
print(k)
set1.add(k)
for j in stu[k]:
print(j,stu[k][j])
set2.add(j)
set3.add(stu[k][j])
print(set1,set2,set3)
output-
a
a1 101
a2 201
b
b1 999
b2 998
{'a', 'b'} {'b1', 'b2', 'a1', 'a2'} {201, 101, 998, 999}
Program to convert set to tuple-->>
ReplyDeletes={2,54,"php","python","java","testing"}
t=tuple(s)
print(t)
output->('testing', 2, 54, 'java', 'php', 'python')
Program to convert tuple to set-->>
ReplyDeletet=("c","c++",111,222,78.90)
s=set(t)
print(s)
output->
{'c++', 78.9, 111, 'c', 222}
program to convert set to list-->>
ReplyDeletes={"dhoni","rohit","shubham",99}
l=list(s)
print(l)
# Multi dimension dictionary
ReplyDeletex = {"name":["Sunil","Anil"],"rno":[101,102], "branch":["cs","it"]}
for i in x:
print(i, end = ' ')
print()
for j in range(0,len(x["name"])):
print(str(x["name"][j]) + " " +str(x["rno"][j])+ " "+ str(x["branch"][j]))
#WAP to convert a dictionary to set and set to the dictionary.
ReplyDeletex = {"name":"Sunny","add":"Indore","number":12345}
a = set()
b= set()
for i in x:
a.add(i)
print(a)
for j in range(0,len(x["name"])):
b.add(x["name"])
b.add(x["add"])
b.add(x["number"])
print(b)
#WAP to convert set to a tuple?
ReplyDeletea = {245,58,69,64}
print(a,type(a))
y=tuple(a)
print(y,type(y))
#WAP to convert tuple to set?
ReplyDeletea = (23,"abc",65,89)
print(a,type(a))
z= set(a)
print(z,type(z))
#######################
b=set()
for i in a:
b.add(i)
print(b,type(b))
#WAP to convert set to list?
ReplyDeletea = {23,45,67,89}
print(a,type(a))
b= list(a)
print(b,type(b))
################################
c=[]
for i in a:
c.append(i)
print(c,type(c))
ReplyDelete# Program for Multi-Dimension Dictionary to Store Student Records and Display Max, Min, Total Fees of Students.
Student = dict()
ids = int(input("How many Student Entries you want to add:-\t"))
# Adding new Students
for j in range(0, ids):
i = input("Enter the Roll No. of Student :-")
Student[i] = {}
# Adding NAME of Student
Student[i]['Name'] = str(input("Enter the name of Student :-")).upper()
# Adding Branch of Student
Student[i][' Branch'] = str(input("Enter the Branch of Student:-")).upper()
# Adding Fee of Student
Student[i]['Fee'] = int(input("Enter the Fee of Student :-"))
lst = Student.items()
res = sorted(Student.items(), key = lambda x: x[1]['Fee'])
print(res)
i = input("Enter the Roll No. of Student :-")
if i in Student:
print("\n",Student[i])
else:
print("Wrong Roll No.")
# wap to dictionary to set
ReplyDeletestu={}
set1=set()
set2=set()
dic=int(input("Enter the number or item in dictionary "))
for j in range(0,dic):
key=input("Enter the key ")
value=input("Enter the value ")
stu.update({key:value})
for i in stu:
set1.add(i)
set2.add(stu[i])
print(set1, set2)
# WaP To dictionary to list
ReplyDeletestu={}
set1=[]
set2=[]
dic=int(input("Enter the number or item in dictionary "))
for j in range(0,dic):
key=input("Enter the key ")
value=input("Enter the value ")
stu.update({key:value})
for i in stu:
set1.append(i)
set2.append(stu[i])
print(set1, set2)
WAP to convert a dictionary to set and set to the dictionary.
ReplyDeleted = {"A":12,"C":34,"B":11,"E":78,"F":22}
set1=set()
set2=set()
for i in d:
set1.add(i)
set2.add(d[i])
print(set1)
print(set2)
set1={'F', 'A', 'B', 'C', 'E'}
d={}
for x in set1:
d.update({x:"hi"})
print(d)
dict1={x:"hi" for x in set1}
print(dict1)
WAP to convert a dictionary to list and list to the dictionary.
ReplyDeleted = {"A":12,"C":34,"B":11,"E":78,"F":22}
l1=[]
l2=[]
for i in d:
l1.append(i)
l2.append(d[i])
print(l1)
print(l2)
a=['A', 'C', 'B', 'E', 'F']
d={}
for i in a:
d.update({i:"hi"})
print(d)
WAP to create a multi-dimensions dictionary
ReplyDeletex=int(input("enter number of records"))
srn=[]
name=[]
fees=[]
for i in range(0,x):
s=int(input("enter srn"))
srn.append(s)
t=(input("enter name"))
name.append(t)
u=int(input("enter fees"))
fees.append(u)
dic={"srn":srn,"name":name,"fees":fees}
print(dic)
WAP to create a multi-dimension set
ReplyDeletea=(1,2,3)
b=(4,5,6)
c=(7,8,9)
s={a,b,c}
print(s)
WAP to convert set to a tuple
ReplyDeleteset1={'F', 'A', 'B', 'C', 'E'}
t=tuple(set1)
print(t)
WAP to convert tuple to set
ReplyDeletet=(1,2,3,4,5)
s=set(t)
print(s)
WAP to convert set to list
ReplyDeleteset1={'F', 'A', 'B', 'C', 'E'}
l=list(set1)
print(l)
WAP to create Mark-sheet Program using Dictionary
ReplyDeletex=int(input("enter number of students"))
dic={}
for i in range(0,x):
n=input("enter name")
e=int(input("enter marks in english"))
m=int(input("enter marks in math"))
dic.update({n:({"english":e,"math":m})})
print(dic)
for i in dic:
print(i)
for j in dic[i]:
print (j,end=" ")
print (dic[i][j])
# WAP to create Mark-sheet Program using Dictionary?
ReplyDeletestu={"Name:-":"Sonu gunhere","Roll number:-":1276,"Class:-":"12th"}
for j in stu:
print(j,stu[j])
s={"phy":85,"chem":88,"hindi":95,"english":82,"math":77}
m=[]
for i in s:
m.append(s[i])
avr=sum(m)/5
if avr>=33 and avr<=47:
print("Pass in C gread ")
if avr>=48 and avr<=59:
print("Pass in B gread ")
if avr>=60 and avr<=80:
print("Pass in A gread ")
if avr>=81 and avr<=100:
print("Passi A* gread")
if avr<33:
print("Fail")
print("percentage is:-",avr,"%")
s={"rno":(1001,1002,1003,1004,1005),"name":["grv","srv","srj","jit","sam"],"branch":["cs","it","bio","sc","ch"],"fees":[25000,40000,30000,35000,50000]}
ReplyDeletefor key in s:
print(key,end='')
print()
for i in range(0,len(s['rno'])):
print(str(s['rno'][i])+" "+s['name'][i]+" "+s['branch'][i]+" "+str(s['fees'][i]))
s={"a":{"a1":10,"a2":20},"b":{"b1":100,"b2":200}}
ReplyDeletes1=set()
s2=set()
s3=set()
for k in s:
print(k)
s1.add(k)
for j in s[k]:
print(j,s[k][j])
s2.add(j)
s3.add(s[k][j])
print(s1,s2,s3)s
ms={}
ReplyDeletess=5
tm=0
for i in range(0,5):
sub=input("enter subject")
marks=int(input("enter obtained marks"))
ms.update({sub:marks})
tm=tm+marks
p=(tm/500)*100
print("your obtained score is",ms)
if p>75:
print("your total percentge is",p,"you are pass with A grade")
elif p<=75 and p>60:
print("your total percentge is",p,"you are pass with B grade")
elif p<=60 and p>45:
print("your total percentge is",p,"you are pass with C grade")
elif p<=45 and p>33:
print("your total percentge is",p,"you are pass with D grade")
else:
print("your are fail your obtained marks are",tm)
dict={'one':1,'two':2,'three':3,'four':4}
Deletelist1=[]
list2=[]
for i in dict:
list1.append(i)
list2.append(dict[i])
print(list1,list2)
stu={"Name:-":"Bharat","Roll number:-":86,"Class:-":"10th"}
ReplyDeletefor j in stu:
print(j,stu[j])
s={"phy":85,"chem":88,"hindi":95,"english":80,"math":98}
m=[]
for i in s:
m.append(s[i])
avr=sum(m)/5
if avr>=33 and avr<=47:
print("Pass in C gread ")
if avr>=48 and avr<=59:
print("Pass in B gread ")
if avr>=60 and avr<=80:
print("Pass in A gread ")
if avr>=81 and avr<=100:
print("Passi A* gread")
if avr<33:
print("Fail")
print("percentage is:-",avr,"%")
#WAP to convert a dictionary to list and list to the dictionary.
ReplyDeletedic = {"A":10,"B":20,"C":33}
lst = []
lst1 = []
for k,v in dic.items():
lst.append(k)
lst1.append(v)
print(lst)
print(lst1)
dic = {}
for data in range(0,len(lst)):
for d in range(data,data+1):
dic.update({lst[data]:lst1[d]})
print(dic)
#WAP to create Mark-sheet Program using Dictionary?
ReplyDeletes1={"phy":55,"chem":32,"hindi":80,"english":80,"math":77}
m=[]
g=0
s=0
sub=""
dis=""
for j,i in s1.items():
if i>27 and i<33:
g=i
s=s+1
sub=sub+","+j
m.append(33-g)
if i>=75 and i<=100:
dis=dis+","+j
m.append(i)
if s==0 or (s==1 and g>=28):
avr=sum(m)/5
if avr>=33 and avr<=45:
print("Pass in C gread ")
elif avr>=45 and avr<=59:
print("Pass in B gread ")
elif avr>=60 and avr<=80:
print("Pass in A gread ")
elif avr>=81 and avr<=100:
print("Passi A gread")
print("percentage is:-",avr,"%")
print("dist sub is ",dis)
print("Pass by grace-",(33-g))
print("grac Subject-",sub)
else:
print("fail with subject-",sub)
# WAP to find Number and Alphabet Special Charecter?
ReplyDeletest="Ankitgautam!@#$#12321gmail.com"
sc=''
num=''
alph=''
for i in range(0,len(st)):
if ord(st[i])>64 and ord(st[i])<91 or ord(st[i])>96 and ord(st[i])<123:
alph=alph+st[i]
elif ord(st[i])>47 and ord(st[i])<59:
num=num+st[i]
else:
sc=sc+st[i]
print(num)
print(sc)
print(alph)
st={'A','C',1,5}
ReplyDeleteb=tuple(st)
print(b)
tpl=(1,2,3,1,2,4)
s=set(tpl)
print(s)
tpl = (11,33,44,3,22)
s=set()
for item in tpl:
s.add(item)
print(s)
lst = [1,2,1,1,2]
s=set()
for item in lst:
s.add(item)
print(s)
#Convert Set To Dictionary
ReplyDeletes1={'sname',branch}
s2={'abc','it'}
dic=dict(zip(s1,s2))
print(dic)
#List To Dictionary
ReplyDeletel1=['rn','sname','branch']
l2=[101,'xyz','cs']
dic=dict(zip(l1,l2))
print(dic)
o/p {'rn':101,'sname':'xyz','branch':'cs'}