使用在列表理解中创建的变量

2024-09-24 02:14:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用在poss = [j.split() for j in [decipher(i, txt) for i in range(1, 26)]]中创建的变量“I”,但当我试图在第return f"The key is {i} and the deciphered text is{' '.join(k)}."行中访问它时,它会引发NameError。有没有办法做到这一点而不引起错误

popular = open("C:/Users/NAME/FilesForPyCharm/popular.txt")
words = [line.strip() for line in popular]
poss = [j.split() for j in [decipher(i, txt) for i in range(1, 26)]]
for k in poss:
    if set(k).issubset(set(words)):
        return f"The key is {i} and the deciphered text is{' '.join(k)}."

Tags: andthekeyintxtforreturnis
1条回答
网友
1楼 · 发布于 2024-09-24 02:14:12

您可以将i的值与其对应的元素一起保存,然后在对上循环:

popular = open("C:/Users/NAME/FilesForPyCharm/popular.txt")
words = [line.strip() for line in popular]
poss = [(j.split(), i) for j in [decipher(i, txt) for i in range(1, 26)]]
for k, i in poss:
    if set(k).issubset(set(words)):
        return f"The key is {i} and the deciphered text is{' '.join(k)}."

相关问题 更多 >