题目:将一个列表的数据复制到另一个列表中
分析:注意浅拷贝和深拷贝
# 将一个列表的数据复制到另一个列表中 a_list = [10, 'dad', 'asdf', 'ewrwer2'] b_list = a_list c_list = a_list[:] import copy d_list = copy.copy(a_list) e_list = copy.deepcopy(a_list) print("修改前:{},{},{},{},{}".format(a_list, b_list, c_list, d_list, e_list)) a_list.append(b_list) print("修改后:{},{},{},{},{}".format(a_list, b_list, c_list, d_list, e_list))
执行结果:
修改前:[10, 'dad', 'asdf', 'ewrwer2'],[10, 'dad', 'asdf', 'ewrwer2'],[10, 'dad', 'asdf', 'ewrwer2'],[10, 'dad', 'asdf', 'ewrwer2'],[10, 'dad', 'asdf', 'ewrwer2'] 修改后:[10, 'dad', 'asdf', 'ewrwer2', [...]],[10, 'dad', 'asdf', 'ewrwer2', [...]],[10, 'dad', 'asdf', 'ewrwer2'],[10, 'dad', 'asdf', 'ewrwer2'],[10, 'dad', 'asdf', 'ewrwer2']