Tag: Print has one of the following tables traversed otherwise contain span ALS dex
Note: This exercise uses the 3.x interpreter
dic = {' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 '}
# Dictionary of methods familiar
# Print (Dic.keys ()) #打印key值
# valuses = dic.values () #打印values
# Print (valuses)
# Print (Dic.items ()) #返回一个包含所有 (key, value) tuple list
# Print (dic.__contains__ (' K1 ')) #如果key在字典中, returns True, otherwise False;python 3.X does not contain the Has_key () function and is __contains__ (key) instead of
#1, please loop through all the keys
For key in Dic.keys ():
print (key)
#2, loop through all the value
For value in Dic.values ():
print (value)
#3, loop through all the keys and the value
For item in Dic.items ():
print ("%s%s"% (Item[0],item[1]))
#4, add a key value to the dictionary, ' K4 ': ' Y4 ', output the added dictionary
dic[' K4 '] = ' y4 '
print (DIC)
#5, please delete a key value in the dictionary, ' K1 ': ' y1 ', Output the deleted dictionary
#del dic[' K1 ']
dic.pop (' K1 ')
print (DIC)
#6, please delete the key ' K5 ' in the dictionary, if there is no key ' K5 ', do not error, and let it return to none
print (' K5 ' in Dic.keys ())
#if ' K5 ' in Dic.keys ():
if dic.__contains__ (' K5 '):
#del dic[' K5 ']
dic.pop (' K5 ')
Else:
Print (Dic.get (' K5 '))
#7, please get the value of ' K2 ' in the dictionary
Print (Dic.get (' K2 '))
#8, get the value of the key ' K6 ' in the dictionary, if the key ' K6 ' does not exist, do not error, and let it return none
Print (Dic.get (' K6 '))
#9, existing dic2 = {' K1 ': ' v111 ', ' a ': ' B '} makes Dic2 = {' K1 ': ' v1 ', ' K1 ': ' v2 ', ' A ': ' B '} through a single line operation
dic2 = {' K1 ': ' v111 ', ' a ': ' B '}
dic2.update (DIC)
#dic. Update (DIC2)
print (DIC2)
#10, combination of nested problems, such as the following table, as required to achieve each function
lis = [[' K ', [' qwe ', 20,{' K1 ': [' TT ', 3, ' 1 ']},89], ' AB ' ]
#10.1 to capitalize the ' TT ' in the list Lis (two ways)
#lis [0][1][2][' k1 '] = [' TT ', 3, ' 1 '] # (1)
lis[0][1][2].get (' K1 ') [0] = ' TT ' # (2)
print (LIS)
#10.2 Convert the list in Lis to the number 3 in the string ' 100 ' (Two ways)
#lis [0][1][2][' k1 '] = [' TT ', ' + ', ' 1 ']# (1)
lis[0][1][2].get (' K1 ') [1] = ' # ' # (2)
print (LIS)
#10.3 Change the list from ' 1 ' to Digital 101 (two ways)
#lis [0][1][2][' k1 '] = [' TT ', ' 3 ', 101] (1)
lis[0][1][2].get (' K1 ') [2] = ' 101 ' # (2)
print (LIS)
#11, there is now a list of li = [All-in-one, ' a ', ' B ', ' 4 ', ' C '], there is a dictionary (this dictionary is
#动态生成的, do not know how many key-value pairs inside, so use dic = {} to emulate this dictionary);
#现在需要完成这样的操作: If the dictionary does not have the ' K1 ' key, then create this ' K1 ' key and
#对应的值 (The key value is set to an empty list), and the index in the list Li is an odd-numbered element, added to ' K1 '
#这个键对应的值中.
Li = [All-in-a, ' a ', ' B ', ' 4 ', ' C ']
dic = {}
dic.setdefault (' K1 ', [])
For I in Li:
if Li.index (i)% 2 = = 1:
dic[' K1 '].append (i)
print (DIC)
python-Dictionary Exercises