This is the code:
def init_dic(init):
return {str(i):init for i in range(0,5)}
a = init_dic([])
print('Origin: ', a)
a['1'].append('test')
print('After append: ', a)
And the result:
Origin: {'3': [], '4': [], '0': [], '1': [], '2': []}
After append: {'3': ['test'], '4': ['test'], '0': ['test'], '1': ['test'], '2': ['test']}
But I expect the result should be:
Origin: {'3': [], '4': [], '0': [], '1': [], '2': []}
After append: {'3': [], '4': [], '0': [], '1': ['test'], '2': []}
Why this happens? I have no idea how so...
So if I want the correct result, how should I do to correct my code?