#python:空列表增加元素时,此时元素也是一个列表,元素列表为何会被改变?
#
str1='45.20.171.50,1.1.1.1'
mylist=[6, 663628395370, '77900247092', 'I', 'IP', '20M', '45.20.171.50,1.1.1.1' ]
mylist1=[]
list6=str1.split(',')
for j in range(0,2):
mylist1.append(mylist)
print(mylist)
print(mylist1)
mylist1[j][6]=list6[j]
print(mylist1)
PS C:\Users\gloria> & "E:/Program Files (x86)/Python310/python.exe" f:/python/1.py
[6, 663628395370, '77900247092', 'I', 'IP', '20M', '45.20.171.50,1.1.1.1']--------------元素列表
[[6, 663628395370, '77900247092', 'I', 'IP', '20M', '45.20.171.50,1.1.1.1']]-------------空列表增加了元素,新列表
[[6, 663628395370, '77900247092', 'I', 'IP', '20M', '45.20.171.50']]------------新列表修改最后一个内容
[6, 663628395370, '77900247092', 'I', 'IP', '20M', '45.20.171.50']-------------元素列表为何为修改了?
[[6, 663628395370, '77900247092', 'I', 'IP', '20M', '45.20.171.50'], [6, 663628395370, '77900247092', 'I', 'IP', '20M', '45.20.171.50']]
[[6, 663628395370, '77900247092', 'I', 'IP', '20M', '1.1.1.1'], [6, 663628395370, '77900247092', 'I', 'IP', '20M', '1.1.1.1']]
----------------------------------------------新列表[0][6]为何也被改了?
PS C:\Users\gloria>
#我想要的结果是:
[6, 663628395370, '77900247092', 'I', 'IP', '20M', '45.20.171.50,1.1.1.1']--------------元素列表
[[6, 663628395370, '77900247092', 'I', 'IP', '20M', '45.20.171.50,1.1.1.1']]-------------空列表增加了元素,新列表
[[6, 663628395370, '77900247092', 'I', 'IP', '20M', '45.20.171.50']]------------新列表修改最后一个内容
经过2次循环后,新列表最终变成下面这样
[[6, 663628395370, '77900247092', 'I', 'IP', '20M', '45.20.171.50'], [6, 663628395370, '77900247092', 'I', 'IP', '20M', '1.1.1.1']]
元素列表内容不变
```python
```