因此,我正在尝试编写一个代码,以获取列表中的每个单词,并用小写或大写(取决于单词)将第一个字母添加到同一单词中
这是代码
lost = ["de", "da", "do"] #The actual list is much bigger and with numbers but this list is small and has the same problem
for x in lost:
if x[:1] == x[:1].lower():
try:
print x
int(x[:1])
except ValueError:
print x
lost.append(x[:1].upper() + x[1:])
elif x[:1] == x[:1].upper():
try:
int(x[:1])
except ValueError:
lost.append(x[:1].lower() + x[1:])
但是在完成所有3个单词之后,for循环再次重新开始,因此控制台如下所示:
de
de
da
da
do
do
de
de
da
da
do
do
....
为什么说完这三个字后它不停止?
答案 0 :(得分:10)
您一次又一次地增加列表的大小,因此要反复循环初始列表,要解决此问题,应将“丢失”的值存储在另一个列表中