在 Python 的嵌套 for 循环中追加新行

append a new line in a nested for loop in Python

我想更改单词列表中字母的颜色。我使用 colorama 库。 代码:

from colorama import Fore
guess = ['great', 'hello', 'brave', 'stone']
li,ly = [],[]
for word in guess:
    for i in range(len(word)):
        if word[i] == 'b' or word[i] == 'a':
            color = Fore.RED
        elif word[i] == 's' or word[i] == 'e':
            color = Fore.GREEN
        else:
            color = Fore.BLACK
        letter = color + word[i] + Fore.RESET
        li.append(letter)
    result = ' '.join(li)
    ly.append(result)
print(*ly, sep = '\n')

输出是

g re a t

g r e a t h e l l o

g r e a t h e l l o b r a v e

g r e a t h e l l o b r a v e s to n e

期望的输出是

g r e a t

g r e a t

h e l l o

g r e a t

h e l l o 

b r a v e

g r e a t 

h e l l o 

b r a v e 

s t o n e

如何在嵌套的 for 循环中添加新行?我还没有找到解决办法。请帮忙。

不确定这是否是您正在寻找的确切解决方案,但我能够通过附加“\n”

来像这样打印它们
from colorama import Fore
guess = ['great', 'hello', 'brave', 'stone']
li,ly = [],[]
for word in guess:
    for i in range(len(word)):
        if word[i] == 'b' or word[i] == 'a':
            color = Fore.RED
        elif word[i] == 's' or word[i] == 'e':
            color = Fore.GREEN
        else:
            color = Fore.BLACK
        letter = color + word[i] + Fore.RESET
        li.append(letter)
    li.append("\n")
    result = ''.join(li)
    ly.append(result.rstrip())
    
print(*ly, sep = '\n')

问题是li包含没有换行的词,每次迭代后没有清除,导致漏掉换行。

解决方案是在每次迭代后清除 li

from colorama import Fore
guess = ['great', 'hello', 'brave', 'stone']
text, line = [], []
for word in guess:
    for character in word:
        if character == 'b' or character == 'a':
            color = Fore.RED
        elif character == 's' or character == 'e':
            color = Fore.GREEN
        else:
            color = Fore.BLACK
        line.append(color + character + Fore.RESET)
    text.append(' '.join(line))
    line.clear()
print(*text, sep = '\n')

如果您想要重复,请使用循环创建它们:

from colorama import Fore
guess = ['great', 'hello', 'brave', 'stone']
text, line = [], []
for word in guess:
    for character in word:
        if character == 'b' or character == 'a':
            color = Fore.RED
        elif character == 's' or character == 'e':
            color = Fore.GREEN
        else:
            color = Fore.BLACK
        line.append(color + character + Fore.RESET)
    text.append(' '.join(line))
    line.clear()
for i in range(1, len(guess) + 1):
    print(*text[:i], sep = '\n')

应该这样做:

guess = ['great', 'hello', 'brave', 'stone']
color_letters = []
spaced_words = []

for word in guess:
    # insert spaces between letters
    for letter in ' '.join(word):

        # identify colour to apply
        if letter in 'ab':
            color = Fore.RED
        elif letter in 'es':
            color = Fore.GREEN
        else:
            color = Fore.BLACK

        # append to cumulative list of formatted letters
        color_letters.append(color + letter + Fore.RESET)

    # insert line breaks between words
    color_letters += '\n\n'

    # add / re-add cumulative list of letters to output list
    spaced_words.append(''.join(color_letters))

print(''.join(spaced_words))