dictionary practice

dic = { ' k1 ' : ' v1 ' , ' k2 ' : ' v2 ' , ' k3 ' : ' v3 ' }
 # ##loop through all keys 
for   key in dic:
     print (key)

# ### Loop through all the values 
​​for values ​​in dic.values():
     print (values)

# ### Loop through all keys and values 
​​for key, value in dic.items():
     print (key, value)

for key in dic:
    print(key,dic[key])

# ###Add a key-value pair to the dictionary, k4: v4 output the added dictionary 
dic[ ' k4 ' ] = ' v4 ' 
print (dic)

# ## Delete the key-value pair k1:v1 in the dictionary and output the deleted dictionary 
del dic[ ' k1 ' ]
 print (dic)

dic.pop('k1')
print(dic)

# ###Delete the key-value pair corresponding to k5 in the dictionary, if k5 does not exist, it will not report an error and return None 
print (dic.pop( ' k5 ' ,None))

# ##Get the value corresponding to k2 in the dictionary 
print (dic[ ' k2 ' ])
 print (dic.get( ' k2 ' ,None))

# ##Get the value corresponding to k6 in the dictionary, if k6 does not exist, return None 
print (dic.get( ' k6 ' ,None))

# ##Existing dic2 = {'k1':'v111','a':'b'} Make dic2 = {'k1':'v1','k2':'v2','k3' by one line operation :'v3','a':'b'} 
dic2 = { ' k1 ' : ' v111 ' , ' a ' : ' b ' }
dic2.update({'k1':'v1','k2':'v2','k3':'v3'})
print(dic2)

'''
10. Combine nested questions, write code, have the following list, and implement each function as required
lis = [['k', ['qwe', 20, {'k1': ['tt', 3, '1']}, 89], 'ab']]
1. Make the 'tt' in the list lis uppercase (in two ways)
2. Turn the number 3 in the list into the string '100' (in two ways)
3 Turn the string '1' in the list into the number 101 (in two ways)
'''
lis = [['k',['qwe',20,{'k1':['tt',3,'1']},89],'ab']]
print(lis[0][1][2].get('k1')[0].upper())
print(lis[0][1][2].get('k1')[0].swapcase())

a = lis[0][1][2].get('k1')[1]='100'
print(a)
dic1 = {'k1':['tt','100','1']}
lis[0][1][2].update(dic1)
print(lis)

a = lis[0][1][2].get('k1')[2]= 101
print(a)
dic1 = {'k1':['tt',3,101]}
lis[0][1][2].update(dic1)
print(lis)

'''
11 Implement the following functions as required:
There is a list li = [1,2,3,'a','b',4,'c'], there is a dictionary
(This dictionary is dynamically generated, you don't know how many key-value pairs there are in it, so use dic = {} to simulate this dictionary);
Now you need to complete the following operations: if the dictionary does not have the key 'k1', create the key 'k1' and its corresponding value (the value corresponding to the key is set to an empty list)
And add the corresponding element with odd index in the list li to the empty list corresponding to the key 'k1'. If the dictionary has the key 'k1',
And the value corresponding to 'k1' is a list type, then add the corresponding element with an odd index in li to the key corresponding to the key 'k1'
'' ' 
li = [1,2,3, ' a ' , ' b ' , 4, ' c ' ]
dic = {}
if  'k1' not in dic:
    dic.setdefault('k1',[])
    for index,i in enumerate(li):
        if index % 2 == 1:
            dic['k1'].append(i)
else:
    if type(dic['k1']) == type([]):
        for i in li:
            if li.index(i) %2 == 1:
                dic['k1'].append(i)
print(dic)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324738366&siteId=291194637