Dictionary

Dictionary common method

 

Copy the code
Dictionary representation: 

info_dic = { ' name ' : ' haiyna ' , ' Age ' : 18 is, ' Sex ' : ' FEMALE ' }
 # common operations: 
#
 1. deposit / fetch 
info_dic = { ' name ' : ' Egon ' , ' Age ' : 18 is, ' Sex ' : ' MALE ' }
   Print (info_dic [ 'name11111111' ]) # Can not find the error of 
  Print (info_dic.get ( ' name ' , None))
   Print (info_dic.get ( ' name222222 ' , None)) # GET method does not find an error, you can set your own default value 

# pOP: Key pop value exists, the absence of a default value is returned if no default value is given 
# Print (info_dic.pop ( 'nam123123123123123123e', None)) 
# Print (info_dic) 

# Print (info_dic.popitem () ) 
# Print (info_dic) 

# info_dic [ 'Level'] = 10 
# Print (info_dic) 

# 
#      deleted 
info_dic = { ' name ' : 'egon','age':18,'sex':'male'}
# info_dic.pop()
# info_dic.popitem()

# del info_dic['name']


#
#     键s,值s,键值对
info_dic={'name':'egon','age':18,'sex':'male'}
# print(info_dic.keys())
# print(info_dic.values())
# print(info_dic.items())

# for k in info_dic:
#     # print(k,info_dic[k])
#     print(k)

# print('========>')
# for k in info_dic.keys():
#     print(k)

# for val in info_dic.values():
#     print(val)

# for k,v in info_dic.items(): #k,v=('name', 'egon')
#     print(k,v)



#     长度
# info_dic={'name':'egon','age':18,'sex':'male'}
# print(len(info_dic))
#
#     循环
#
#     包含in

# info_dic={'name':'egon','age':18,'sex':'male'}
# print('name' in info_dic)
# print('name' in info_dic.keys())
# print('egon' in info_dic.values())
# print(('name','egon') in info_dic.items())



#掌握
info_dic={'name':'egon','age':18,'sex':'male'}
# info_dic.update({'a':1,'name':'Egon'}) 
#info_dic [ 'hobbies'] = []#Print (info_dic)
#

info_dic [ 'hobbies'] the append ( 'Study'). 
# info_dic [ 'hobbies'] the append ( 'Read'). 
# Print (info_dic) 

# setDefault: Key does not exist set a default value and the default value back 
# key is not present a default setting, and returns any value already 

# info_dic.setdefault ( 'hobbies', [1,2]) 
# Print (info_dic) 
# info_dic.setdefault ( 'hobbies', [1,2,3,4 ,. 5]) 
# Print (info_dic) 

# info_dic = { 'name': 'Haiyan', 'Age': 18 is, 'Sex': 'MALE'} 

# { 'name': 'Egon', 'Age': 18 is , 'Sex': 'MALE', 'hobbies': [ 'Study']} 
# . info_dic.setdefault ( 'hobbies', []) the append ( 'Study')

# {'name':'egon','age':18,'sex':'male','hobbies':['study','read']}
# info_dic.setdefault('hobbies',[]).append('read')

# {'name':'egon','age':18,'sex':'male','hobbies':['study','read','sleep']}
# info_dic.setdefault('hobbies',[]).append('sleep')
# l=info_dic.setdefault('hobbies',[])
# print(l,id(l))
# print(id(info_dic['hobbies']))

# print(info_dic)



#了解
# d=info_dic.copy()
# print(d)
# info_dic.clear()
# print(info_dic)


# d=info_dic.fromkeys(('name','age','sex'),None)
# print(d)
# d1=dict.fromkeys(('name','age','sex'),None)
# d2=dict.fromkeys(('name','age','sex'),('egon',18,'male'))
# print(d1)
# print(d2)




# info=dict(name='haiyan',age=18,sex='male')
# print(info)

#
# info=dict([('name','haiyan'),('age',18)])
# print(info)
  
Copy the code

 

Dictionary common method

 

Copy the code
Dictionary representation: 

info_dic = { ' name ' : ' haiyna ' , ' Age ' : 18 is, ' Sex ' : ' FEMALE ' }
 # common operations: 
#
 1. deposit / fetch 
info_dic = { ' name ' : ' Egon ' , ' Age ' : 18 is, ' Sex ' : ' MALE ' }
   Print (info_dic [ 'name11111111' ]) # Can not find the error of 
  Print (info_dic.get ( ' name ' , None))
   Print (info_dic.get ( ' name222222 ' , None)) # GET method does not find an error, you can set your own default value 

# pOP: Key pop value exists, the absence of a default value is returned if no default value is given 
# Print (info_dic.pop ( 'nam123123123123123123e', None)) 
# Print (info_dic) 

# Print (info_dic.popitem () ) 
# Print (info_dic) 

# info_dic [ 'Level'] = 10 
# Print (info_dic) 

# 
#      deleted 
info_dic = { ' name ' : 'egon','age':18,'sex':'male'}
# info_dic.pop()
# info_dic.popitem()

# del info_dic['name']


#
#     键s,值s,键值对
info_dic={'name':'egon','age':18,'sex':'male'}
# print(info_dic.keys())
# print(info_dic.values())
# print(info_dic.items())

# for k in info_dic:
#     # print(k,info_dic[k])
#     print(k)

# print('========>')
# for k in info_dic.keys():
#     print(k)

# for val in info_dic.values():
#     print(val)

# for k,v in info_dic.items(): #k,v=('name', 'egon')
#     print(k,v)



#     长度
# info_dic={'name':'egon','age':18,'sex':'male'}
# print(len(info_dic))
#
#     循环
#
#     包含in

# info_dic={'name':'egon','age':18,'sex':'male'}
# print('name' in info_dic)
# print('name' in info_dic.keys())
# print('egon' in info_dic.values())
# print(('name','egon') in info_dic.items())



#掌握
info_dic={'name':'egon','age':18,'sex':'male'}
# info_dic.update({'a':1,'name':'Egon'}) 
#info_dic [ 'hobbies'] = []#Print (info_dic)
#

info_dic [ 'hobbies'] the append ( 'Study'). 
# info_dic [ 'hobbies'] the append ( 'Read'). 
# Print (info_dic) 

# setDefault: Key does not exist set a default value and the default value back 
# key is not present a default setting, and returns any value already 

# info_dic.setdefault ( 'hobbies', [1,2]) 
# Print (info_dic) 
# info_dic.setdefault ( 'hobbies', [1,2,3,4 ,. 5]) 
# Print (info_dic) 

# info_dic = { 'name': 'Haiyan', 'Age': 18 is, 'Sex': 'MALE'} 

# { 'name': 'Egon', 'Age': 18 is , 'Sex': 'MALE', 'hobbies': [ 'Study']} 
# . info_dic.setdefault ( 'hobbies', []) the append ( 'Study')

# {'name':'egon','age':18,'sex':'male','hobbies':['study','read']}
# info_dic.setdefault('hobbies',[]).append('read')

# {'name':'egon','age':18,'sex':'male','hobbies':['study','read','sleep']}
# info_dic.setdefault('hobbies',[]).append('sleep')
# l=info_dic.setdefault('hobbies',[])
# print(l,id(l))
# print(id(info_dic['hobbies']))

# print(info_dic)



#了解
# d=info_dic.copy()
# print(d)
# info_dic.clear()
# print(info_dic)


# d=info_dic.fromkeys(('name','age','sex'),None)
# print(d)
# d1=dict.fromkeys(('name','age','sex'),None)
# d2=dict.fromkeys(('name','age','sex'),('egon',18,'male'))
# print(d1)
# print(d2)




# info=dict(name='haiyan',age=18,sex='male')
# print(info)

#
# info=dict([('name','haiyan'),('age',18)])
# print(info)
  
Copy the code

 

Guess you like

Origin www.cnblogs.com/maaosheng/p/11619101.html