python - for 循环不断地重新开始

标签 python list for-loop

所以我试图制作一个代码,获取列表中的每个单词,并添加相同的单词,第一个字母为小写或大写(取决于单词)

这是代码

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/

相关文章:

javascript - 在 for 循环中使用现有变量

python - Tornado 中的队列和 ProcessPoolExecutor

python - GAE : Task on backend instance killed without warning

arrays - JDK 的数组与 Guava 的 ImmutableList

r - 如何在R中没有for循环的情况下将一列与一系列相关的虚拟变量进行比较

Java For循环三角形图案

python - 通过Windows Task Scheduler 调用python 脚本时,如何正确导入用户定义的模块(包)?

python - 不均匀(曲线)网格的 Matplotlib 流图

python - 定义获取列表中最高产品对的函数

java - 完美的数字