我写了这个脚本:
soilMod = ['ptaaco']
n = 2
soilModSplit = [soilMod[i:i+n] for i in range(0, len(soilMod), n)] # This returns ['pt', 'aa', 'co']
alphaTest = soilModSplit # I want to maintain the original list built before I remove items below. This returns ['pt', 'aa', 'co'] as well
if 'pt' in soilModSplit:
soilModSplit.remove('pt')
print soilModList # This returns ['aa', 'co']
print alphaTest # This also returns ['aa', 'co'] It's missing the original ['pt', 'aa', 'co'] and I didn't ask the script to remove it from this particular list.
出于某种原因,当我从 soilModSplit 中删除项目 ('pt') 时,它也会从 alphaTest 中删除该项目。这是预期的结果吗?似乎建立在 soilModSplit 上的任何变量(在本例中为 alphaTest)都依赖于对 soilModSplit 采取的任何操作。也许我做错了什么?有没有办法解决这个问题?
谢谢,迈克