所以我试图制作一个代码,获取列表中的每个单词,并添加相同的单词,第一个字母为小写或大写(取决于单词)
这是代码
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
....
为什么它做完这三个字后还不停止?
最佳答案
您一遍又一遍地增加列表的大小,因此您一次又一次地循环使用初始列表,以解决您应该将“丢失”值存储在不同列表中的问题
关于python - for 循环不断地重新开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60308808/