In [1]:
dict={}
dict
Out[1]:
In [2]:
#key must be immutable type
dict1={1:'jan',2:'feb',3:'mar',4:'april'}
dict1
Out[2]:
In [2]:
dict={'abc':[2,3]}
dict
Out[2]:
In [4]:
#dictinaryname[key] gives values for concerned key
dict1[1]
Out[4]:
In [4]:
#element in dictionary is not in ordered faishon like list tuple and sting
d={1:'a',2:'c',3:'b'}
d[2]
Out[4]:
In [7]:
print(d)
In [8]:
print(d[1],d[3])
In [9]:
d1={'six':'computer','seven':'physiscs','eight':'chemistry'}
d1
Out[9]:
In [11]:
d1['seven']
Out[11]:
In [5]:
#dictionary traversal
d1={'six':'computer','seven':'physiscs','eight':'chemistry'}
for key in d1:
print(key,d1[key])
In [7]:
#dictionary key print
print(d1.keys())
In [16]:
d1.values()
Out[16]:
In [18]:
list(d1.keys())
Out[18]:
In [19]:
list(d1.values())
Out[19]:
In [21]:
#3dictionary values can be accessed by its key values ,not by any index
dic={12:'hello','abc':12,(4,5):[5,6]}
print(dic[12],dic['abc'],dic[(4,5)])
In [23]:
#keys can be dupicate but it must be unique
dic={12:13,12:14}
print(dic)
In [8]:
#values can be duplicate
dic={13:13,14:13,15:13}
dic
Out[8]:
In [9]:
#chaning value at key location in dictionary
dic[13]='abc'
dic
Out[9]:
In [10]:
#adding new element in dictionary
dic[16]='def'
dic
Out[10]:
In [30]:
dic
Out[30]:
In [34]:
#empty dictionary creation
d={}
d
Out[34]:
In [35]:
d[45]='abc'
d[60]='def'
d
Out[35]:
In [12]:
emp=dict(name='abc',salary=5000,age=24)
emp
In [37]:
emp=dict((('name','john',('salary',1000),('age',24))))
emp
In [17]:
#simple program making dictionary for name and numberof centuary of cricketer
n=int(input('enter how many cricketer'))
centrecord={}
for a in range(n):
key=input('name of cricketer')
value=int(input('numeber of centuary'))
centrecord[key]=value
print('the dictionary now is ',centrecord)
In [12]:
#deletion of element in key
d={'a':1,'b':2,'c':3}
del(d['a'])
d['d']=9
print(d.pop('b'))
print(d)
In [13]:
#key existence
print('c' in d)
print('d' not in d)
print(3 in d)
print(3 in d.values())
In [14]:
#count every word in string and make dictionary to store that
s='hello hello hello hi hi hi'
w=s.split()
d={}
for one in w:
k=one
if k not in d:
c=w.count(k)
d[k]=c
print('count frequency',w)
print(d)
In [8]:
#deletion of element in dictionary
d={'a':1,'b':2,'c':3}
del(d['a'])
d
Out[8]:
In [14]:
print(len(d))
In [17]:
d.clear()
d
Out[17]:
In [20]:
del d
In [24]:
#deletion of dictionary
di={1:2,2:3,3:4}
de
In [27]:
#deletion of contained of dictionary
di={1:2,2:3,3:4}
di.clear()
di
Out[27]:
In [16]:
#get method
di={1:'virat',2:'rohit',3:'dhavan',4:'hardik'}
print(di.get(1,"not available "))
print(di.get(5,"not in indian team"))
print(di.get('virat','not in team'))
In [17]:
#items method
di={1:'virat',2:'rohit',3:'dhavan',4:'hardik'}
l=di.items()
for i,j in l:
print(i,':',j)
In [19]:
#items method
di={1:'virat',2:'rohit',3:'dhavan',4:'hardik'}
l=di.items()
for i,j in l:
print(i,'position cricketer',j)
In [23]:
#update method
di={1:'virat',2:'rohit',3:'dhavan',6:'hardik'}
di1={1:'sachin',2:'saurabh',3:'dhoni',4:'bhuvneshwar',5:'dev'}
di1.update(di)
print(di)
print(di1)
In [43]:
di={1:'virat',2:'rohit',3:'dhavan',4:'hardik'}
di1={1:'sachin',3:'saurabh',5:'dhoni',7:'bhuvneshwar'}
di.update(di1)
print(di)
In [50]:
l=list(di.keys())
print(l[1])
k=list(di.values())
print(k[1])
In [51]:
l=tuple(di.keys())
print(l[1])
k=tuple(di.values())
print(k[1])
In [4]:
d={'india':[3,4]}
l=d['india']
l[1]
Out[4]:
In [7]:
dm={'jan':31,'feb':28,'mar':30}
for m in dm:
print(m)
In [8]:
dm={'jan':31,'feb':28,'mar':30}
for m in dm:
print(dm[m])
In [9]:
dm={'jan':31,'feb':28,'mar':30}
for m in dm:
if(dm[m]==31):
print(m)
In [10]:
daylist=list(dm.values())
daylist
Out[10]:
In [12]:
daylist=list(dm.values())
daylist.sort()
daylist
Out[12]:
In [15]:
D2={2:'b',3:'c',5:'e'}
D1={1:'a',2:'b',3:'c',4:'d'}
l1=len(D1)
l2=len(D2)
if(l1>l2):
for k in D1:
print(D2.get(k))
else:
for k in D2:
print(D1.get(k))
In [18]:
#13C6
D2={2:'b',3:'c',5:'e'}
D1={1:'a',2:'b',3:'c',4:'d'}
l1=len(D1)
l2=len(D2)
ol=[]
if(l1>l2):
for k in D1:
if((D2.get(k)!=None)):
ol.append(k)
else:
for k in D2:
if((D1.get(k)!=None)):
ol.append(k)
print(ol)
In [20]:
#13c5 inverted dictionary
d={1:'A',2:'B',3:'C'}
rd={}
for k in d:
rd[d[k]]=k
print (rd)
In [5]:
d={'ind':[4,2],'pak':[5,3],'aus':[8,2]}
s=0
t=''
for a in d:
l=d[a]
if(s<l[1]):
s=l[1]
t=a
print(t,'is lost highest match ',s)
In [ ]:
No comments:
Post a Comment