#-*-Coding:utf-8-*-
Write code, have the following dictionary, as required to implement each function dic={' k1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 '}
1. Loop through all the keys:
dic={' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 '}
Print (Dic.keys ())
For key in Dic.items ():
Print (Dic.keys ())
2. Loop through all the value:
dic={' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 '}
Print (Dic.values ())
For key in Dic.items ():
Print (Dic.values ())
3. Loop through all the keys and the value:
dic={' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 '}
For k,v in Dic.items ():
Print (K,V)
4. Add a key-value pair to the dictionary, ' K4 ': ' v4 ', output the added dictionary
dic={' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 '}
dic[' K4 ']= ' v4 '
Print (DIC)
5. Remove the dictionary from the key-value pair ' k1 ', ' v1 ', and output the deleted dictionary
dic={' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 '}
Del dic[' K1 ']
Print (DIC)
6. Delete the key ' K5 ' corresponding to the dictionary key value pair, if the dictionary does not exist in the key ' K5 ', then no error, and let it return to none
dic={' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 '}
Dic.pop (' K5 ')
Print (DIC)
7. Please get the value of ' K2 ' in the dictionary
dic={' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 '}
Dic.get (' K2 ')
Print (Dic.get (' K2 '))
8. Please get the value of ' K6 ' in the dictionary, if the key ' K6 ' does not exist, do not error, and let it return none
dic={' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 '}
Dic.get (' K6 ')
Print (Dic.get (' K6 '))
9. Existing Dic2 = {' K1 ': ' v111 ', ' a ': ' B '} makes Dic2 = {' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 ', ' a ': ' B '} through a single line operation
dic={' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 '}
Dic2 = {' K1 ': ' v111 ', ' a ': ' B '}
Dic2.update (DIC)
Print (DIC2)
10. Combine nested questions. Write code, such as the following list, as required to achieve each function
lis = [[' K ', [' qwe ', 20,{' K1 ': [' TT ', 3, ' 1 ']},89], ' AB ']
1. Capitalize the ' TT ' in the LIS list (in two ways)
Method One:
lis = [[' K ', [' qwe ', +, {' K1 ': [' TT ', 3, ' 1 ']}, [], ' AB ']
lis[0][1][2][' k1 '][0] = lis[0][1][2][' K1 '][0].upper ()
Print (LIS)
Method Two:
lis = [[' K ', [' qwe ', +, {' K1 ': [' TT ', 3, ' 1 ']}, [], ' AB ']
Lis[0][1][2].get (' K1 ') [0] = lis[0][1][2][' K1 '][0].upper ()
Print (LIS)
2. Change the number 3 in the list to the string ' 100 ' (in two ways)
Method One:
lis = [[' K ', [' qwe ', +, {' K1 ': [' TT ', 3, ' 1 ']}, [], ' AB ']
lis[0][1][2][' k1 '][1] = ' 100 '
Print (LIS)
Method Two:
lis = [[' K ', [' qwe ', +, {' K1 ': [' TT ', 3, ' 1 ']}, [], ' AB ']
Lis[0][1][2].get (' K1 ') [1] = ' 100 '
Print (LIS)
3. Change the string ' 1 ' in the list to the number 101 (in two ways)
Method One:
lis = [[' K ', [' qwe ', +, {' K1 ': [' TT ', 3, ' 1 ']}, [], ' AB ']
lis[0][1][2][' k1 '][2] = 101
Print (LIS)
Method Two:
lis = [[' K ', [' qwe ', +, {' K1 ': [' TT ', 3, ' 1 ']}, [], ' AB ']
Lis[0][1][2].get (' K1 ') [2] = 101
Print (LIS)
11. The following functions are implemented as required:
There is a list of li = [three-way, ' A ', ' B ', 4, ' C '], there is a dictionary (this dictionary is dynamically generated, you do not know how many key-value pairs are in it, so use dic={} to emulate this dictionary); Now you need to do this:
If the dictionary does not have the ' K1 ' key, create the ' K1 ' key and its value (the value of the key is set to an empty list), and the index in the list Li is an odd-numbered element that is added to the empty list of the ' K1 ' key.
If the dictionary has the ' K1 ' key and the K1 corresponding value is a list type, then the index in the list Li is an even-numbered element that is added to the value corresponding to the ' K1 ' key.
Li = [All-in-a, ' a ', ' B ', 4, ' C ']
LI1 = []
DiC = {' K1 ': li1}
If ' K1 ' not in dic:
For I in Li:
If Li.index (i)% 2 = = 1:
Li1.append (i)
Print (LI1)
dic[' k1 '] = Li1
Print (DIC)
Else
If Type (li1) = = List:
For I in Li:
If Li.index (i)% 2 = = 0:
Li1.append (i)
dic[' k1 '] = Li1
Print (DIC)
# 12. Known string a = "aasmr3idd4bgs7dlsf9eaf", required as follows
# 12.1 Change the uppercase of a string to lowercase and lowercase to uppercase.
A = ' aasmr3idd4bgs7dlsf9eaf '
A = A.swapcase ()
Print (a)
# 12.2 Please take the number of a string out and output it as a new string.
A = ' aasmr3idd4bgs7dlsf9eaf '
L = []
For S in a:
If S.isdigit ():
L.append (s)
Print (L)
Print (". Join (L)")
Print (". Join" ([s for S in a If S.isdigit ()]))
# 12.3 Please count the occurrences of each letter a string appears (ignoring case, a and a are the same letter) and outputting it as a dictionary. Example {' A ': 4, ' B ': 2}
A = ' aasmr3idd4bgs7dlsf9eaf '
A = A.lower ()
b = {}
For s in Set (a):
B[s] = A.count (s)
Print (b)
Python beginner Nineth Day string, list, dictionary exercises