Data traversal can't be silly series
Preface:
When doing some operations on dictionaries, we sometimes encounter the following situations, such as we need to replace the key value in data according to the mapping relationship in replace (Caller is replaced by caller)
data = { 'Caller': '01053180888', 'CompId': '800999','Callee': '13911517997','RingTime': '1', } replace ={ 'Caller': 'caller', 'CompId': 'compid','Callee': 'callee','RingTime': 'ring_time'}
Of course, I know that dictionaries can't delete when they are traversing, otherwise they will make mistakes.
data = {'name':'18'} for name in data: value = data[name] del data[name] data['age'] = value print(data) # {'age': '18'}
There is no error at all, so follow this way to achieve just the needs, haha ha, and then have the following code
data = { 'Caller': '01053180888', 'CompId': '800999','Callee': '13911517997','RingTime': '1', } replace ={ 'Caller': 'caller', 'CompId': 'compid','Callee': 'callee','RingTime': 'ring_time'} for key in data: value = data[key] if key in replace: del data[key] data[replace[key]] = value print(data) # {'Callee': '13911517997', 'RingTime': '1', 'caller': '01053180888', 'compid': '800999'}
Half of the content has not been replaced, ha ha ha, face, although not wrong, but did not achieve the desired effect. Analysis Principle: The reason for no error is that deleting key in the loop and then adding a key, the ladder length remains unchanged, no error is reported, but because the key value has changed, all keys in the next cycle may be the new key value.
Since you can't delete the key value while traversing the dictionary, then we can cycle keys directly and add or delete the data. It's not without affecting the wit (applause), and then we have the following code.
data = { 'Caller': '01053180888', 'CompId': '800999','Callee': '13911517997','RingTime': '1', } replace ={ 'Caller': 'caller', 'CompId': 'compid','Callee': 'callee','RingTime': 'ring_time'} keys = data.keys() for key in keys: print(key) value = data[key] if key in replace: del data[key] data[replace[key]] = value print(data) {'Callee': '13911517997', 'RingTime': '1', 'caller': '01053180888', 'compid': '800999'}
It's very unscientific to grip and slap your face again. Why? It's clear that keys have no relationship with data. Print out the type of keys and find it's different from what you imagine.
keys = data.keys() print(type(keys)) # <class 'dict_keys'>
Keys are not a list at all, dict_keys will change based on data
The first way is:
Passing in the dictionary with the list function will generate a list based on the dictionary key value, which will be solved perfectly (applause)
data = { 'Caller': '01053180888', 'CompId': '800999','Callee': '13911517997','RingTime': '1', } replace ={ 'Caller': 'caller', 'CompId': 'compid','Callee': 'callee','RingTime': 'ring_time'} for key in list(data): value = data[key] if key in replace: del data[key] data[replace[key]] = value print(data) {'caller': '01053180888', 'compid': '800999', 'callee': '13911517997', 'ring_time': '1'}
The second way is:
Three times in my heart, I don't seem silly... Why do I have to recycle data?! Look at the following, this code is simple and angry, unexpectedly, I wonder if I am not suitable for development, convex.....................................................
data = { 'Caller': '01053180888', 'CompId': '800999','Callee': '13911517997','RingTime': '1', } replace ={ 'Caller': 'caller', 'CompId': 'compid','Callee': 'callee','RingTime': 'ring_time'} for key in replace: value = data.pop(key) data[replace[key]] = value print(data) {'caller': '01053180888', 'compid': '800999', 'callee': '13911517997', 'ring_time': '1'}