Tuesday, June 30, 2020

Dictionary Practice in Python

dictionary
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]:
{1: 'jan', 2: 'feb', 3: 'mar', 4: 'april'}
In [2]:
dict={'abc':[2,3]}
dict
Out[2]:
{'abc': [2, 3]}
In [4]:
#dictinaryname[key] gives values for concerned key
dict1[1]
Out[4]:
'jan'
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]:
'c'
In [7]:
print(d)
{1: 'a', 2: 'c', 3: 'b'}
In [8]:
print(d[1],d[3])
a b
In [9]:
d1={'six':'computer','seven':'physiscs','eight':'chemistry'}
d1
Out[9]:
{'six': 'computer', 'seven': 'physiscs', 'eight': 'chemistry'}
In [11]:
d1['seven']
Out[11]:
'physiscs'
In [5]:
#dictionary traversal
d1={'six':'computer','seven':'physiscs','eight':'chemistry'}

for key in d1:
     print(key,d1[key])
six computer
seven physiscs
eight chemistry
In [7]:
#dictionary key print
print(d1.keys())
dict_keys(['six', 'seven', 'eight'])
In [16]:
d1.values()
Out[16]:
dict_values(['computer', 'physiscs', 'chemistry'])
In [18]:
list(d1.keys())
Out[18]:
['six', 'seven', 'eight']
In [19]:
list(d1.values())
Out[19]:
['computer', 'physiscs', 'chemistry']
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)])
hello 12 [5, 6]
In [23]:
#keys can be dupicate but it must be unique
dic={12:13,12:14}
print(dic)
{12: 14}
In [8]:
#values can be duplicate
dic={13:13,14:13,15:13}
dic
Out[8]:
{13: 13, 14: 13, 15: 13}
In [9]:
#chaning value at key location in dictionary
dic[13]='abc'
dic
Out[9]:
{13: 'abc', 14: 13, 15: 13}
In [10]:
#adding new element in dictionary
dic[16]='def'
dic
Out[10]:
{13: 'abc', 14: 13, 15: 13, 16: 'def'}
In [30]:
dic
Out[30]:
{13: 'abc', 14: 15, 16: 'def'}
In [34]:
#empty dictionary creation
d={}
d
Out[34]:
{}
In [35]:
d[45]='abc'
d[60]='def'
d
Out[35]:
{45: 'abc', 60: 'def'}
In [12]:
emp=dict(name='abc',salary=5000,age=24)
emp
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-391ff9a4acb8> in <module>
----> 1 emp=dict(name='abc',salary=5000,age=24)
      2 emp

TypeError: 'dict' object is not callable
In [37]:
emp=dict((('name','john',('salary',1000),('age',24))))
emp
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-37-41183db994f3> in <module>
----> 1 emp=dict((('name','john',('salary',1000),('age',24))))
      2 emp

TypeError: 'dict' object is not callable
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)
enter how many cricketer6
name of cricketerganga
numeber of centuary-1
name of cricketeranukrati
numeber of centuary-88
name of cricketerprachi
numeber of centuary50
name of cricketersonam
numeber of centuary9
name of cricketerpalak
numeber of centuary101
name of cricketersejal
numeber of centuary45
the dictionary now is  {'ganga': -1, 'anukrati': -88, 'prachi': 50, 'sonam': 9, 'palak': 101, 'sejal': 45}
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)
2
{'c': 3, 'd': 9}
In [13]:
#key existence
print('c' in d)
print('d' not in d)
print(3 in d)
print(3 in d.values())
True
False
False
True
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)
count frequency ['hello', 'hello', 'hello', 'hi', 'hi', 'hi']
{'hello': 3, 'hi': 3}
In [8]:
#deletion of element in dictionary
d={'a':1,'b':2,'c':3}
del(d['a'])
d
Out[8]:
{'b': 2, 'c': 3}
In [14]:
print(len(d))
2
In [17]:
d.clear()
d
Out[17]:
{}
In [20]:
del d
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-20-b048573058e5> in <module>
----> 1 del d

NameError: name 'd' is not defined
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'))
virat
not in indian team
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)
1 : virat
2 : rohit
3 : dhavan
4 : hardik
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)
1 position cricketer virat
2 position cricketer rohit
3 position cricketer dhavan
4 position cricketer hardik
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)
{1: 'virat', 2: 'rohit', 3: 'dhavan', 6: 'hardik'}
{1: 'virat', 2: 'rohit', 3: 'dhavan', 4: 'bhuvneshwar', 5: 'dev', 6: 'hardik'}
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)
{1: 'sachin', 2: 'rohit', 3: 'saurabh', 4: 'hardik', 5: 'dhoni', 7: 'bhuvneshwar'}
In [50]:
l=list(di.keys())
print(l[1])
k=list(di.values())
print(k[1])
2
rohit
In [51]:
l=tuple(di.keys())
print(l[1])
k=tuple(di.values())
print(k[1])
2
rohit
In [4]:
d={'india':[3,4]}
l=d['india']
l[1]
Out[4]:
4
In [7]:
dm={'jan':31,'feb':28,'mar':30}
for m in dm:
    print(m)
jan
feb
mar
In [8]:
dm={'jan':31,'feb':28,'mar':30}
for m in dm:
    print(dm[m])
31
28
30
In [9]:
dm={'jan':31,'feb':28,'mar':30}
for m in dm:
    if(dm[m]==31):
        print(m)
jan
In [10]:
daylist=list(dm.values())
daylist
Out[10]:
[31, 28, 30]
In [12]:
daylist=list(dm.values())
daylist.sort()
daylist
Out[12]:
[28, 30, 31]
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))
None
b
c
None
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)
[2, 3]
In [20]:
#13c5 inverted dictionary
d={1:'A',2:'B',3:'C'}
rd={}
for k in d:
    rd[d[k]]=k
print (rd)
    
{'A': 1, 'B': 2, 'C': 3}
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)
pak is lost highest match  3
In [ ]:
 

No comments:

Post a Comment