我有一个列表如下所示:
voting_records = [['1.', 'DIRECTOR', 'Management'],
['1', 'Allison Grant Williams', 'For'],
['2', 'Sheila Hartnett-Devlin', 'For'],
['3', 'James Jessee', 'For'],
['4', 'Teresa Polley', 'For'],
['5', 'Ashley T. Rabun', 'For'],
['6', 'James E. Ross', 'For'],
['7', 'Rory Tobin', 'For']]
我使用列表理解创建了一个名为
voting_records_short
的列表副本:
cnames = ["Record","Proposal","Sponsor","VoteCast"]
voting_records_short = [record for record in voting_records if len(record) < len(cnames)]
然后我创建了一片
voting_records_short
并保存在directors
中。然后我在 for 循环中修改 directors
列表,如下所示:
if len(voting_records_short) == 0: pass
else:
ifdirector = ["DIRECTOR" in record for record in voting_records_short]
if True in ifdirector:
directorindx = int(ifdirector.index(True))
directorline = [voting_records_short[directorindx]]
directors = voting_records_short[directorindx+1:]
sponsoridx = cnames.index("Sponsor")
for ls in range(len(directors)):
directors[ls][0] = directorline[0][0] + directors[ls][0]
directors[ls][1] = directorline[0][1] + " " +directors[ls][1]
directors[ls].insert(sponsoridx,directorline[0][-1])
为什么在 for 循环中修改导演列表会同时修改
voting_records_short
列表和 voting_records
,如下面的输出所示:
In [2]: voting_records
Out[2]:
[['1.', 'DIRECTOR', 'Management'],
['1.1', 'DIRECTOR Allison Grant Williams', 'Management', 'For'],
['1.2', 'DIRECTOR Sheila Hartnett-Devlin', 'Management', 'For'],
['1.3', 'DIRECTOR James Jessee', 'Management', 'For'],
['1.4', 'DIRECTOR Teresa Polley', 'Management', 'For'],
['1.5', 'DIRECTOR Ashley T. Rabun', 'Management', 'For'],
['1.6', 'DIRECTOR James E. Ross', 'Management', 'For'],
['1.7', 'DIRECTOR Rory Tobin', 'Management', 'For']]
In [9]: voting_records_short
Out[9]:
[['1.', 'DIRECTOR', 'Management'],
['1.1', 'DIRECTOR Allison Grant Williams', 'Management', 'For'],
['1.2', 'DIRECTOR Sheila Hartnett-Devlin', 'Management', 'For'],
['1.3', 'DIRECTOR James Jessee', 'Management', 'For'],
['1.4', 'DIRECTOR Teresa Polley', 'Management', 'For'],
['1.5', 'DIRECTOR Ashley T. Rabun', 'Management', 'For'],
['1.6', 'DIRECTOR James E. Ross', 'Management', 'For'],
['1.7', 'DIRECTOR Rory Tobin', 'Management', 'For']]
我原以为只有
directors
列表会被修改(也许voting_records_short
但我不确定)但是为什么原来的voting_records
列表被修改了?我会很感激任何帮助?
您正在迭代原始的 voting_records 并将对这些记录中的每一个的引用存储在一个新列表中;因此,对带有引用的记录所做的更改将修改两个列表——如上所述:deepcopy
from copy import deepcopy
copy_records = deepcopy(voting_records)
voting_records_short = [record for record in copy_records if len(record) < len(cnames)]
运行代码后:
[['1.', 'DIRECTOR', 'Management'], ['1', 'Allison Grant Williams', 'For'], ['2', 'Sheila Hartnett-Devlin', 'For'], ['3', 'James Jessee', 'For'], ['4', 'Teresa Polley', 'For'], ['5', 'Ashley T. Rabun', 'For'], ['6', 'James E. Ross', 'For'], ['7', 'Rory Tobin', 'For']]
[['1.', 'DIRECTOR', 'Management'], ['1.1', 'DIRECTOR Allison Grant Williams', 'Management', 'For'], ['1.2', 'DIRECTOR Sheila Hartnett-Devlin', 'Management', 'For'], ['1.3', 'DIRECTOR James Jessee', 'Management', 'For'], ['1.4', 'DIRECTOR Teresa Polley', 'Management', 'For'], ['1.5', 'DIRECTOR Ashley T. Rabun', 'Management', 'For'], ['1.6', 'DIRECTOR James E. Ross', 'Management', 'For'], ['1.7', 'DIRECTOR Rory Tobin', 'Management', 'For']]