【发布时间】:2021-07-07 17:45:55
【问题描述】:
我只想通过使用 lower() 以小写形式添加列表的每个条目。 我正在使用这段代码来完成任务:
MyList = ["EntryOne", "EntryTwo"]
TempList = MyList #cloning MyList to TempList
for v in TempList:
MyList.append(v.lower()) #Why is it also being appended into TempList ?
print(MyList)
print(TempList)
#expected output:
#["EntryOne", "EntryTwo", "entryone", "entrytwo"]
#["EntryOne", "EntryTwo"]
如您所见,TempList 的声明在 for 循环之外,我只是在开头声明它。没有将小写字母附加到 TempList 的代码。
因此这个脚本永远循环。
【问题讨论】:
-
无处你克隆了一个列表。
TempList = MyList从不复制任何东西。这只是将相同的列表对象分配给另一个变量。
标签: python python-3.x