我只想使用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中。
因此,这个脚本将永远循环。
您的错误是第2行中的语句:
TempList = MyList #cloning MyList to TempList
这是不正确的。它不会克隆您的列表。
改为使用.copy()
:
TempList = MyList.copy()