我有一本字典,里面全是名为“可能”的列表,键为“a”-“z”。每个键都有一个列表的值,其中包含“a”到“z”。本质上,26 个字母。
我将一个字符串清理成小写并去除标点符号并将每个单词保存到一个名为“cleanedWords”的列表中。
我想遍历列表,如果列表中的单词只有两个字母,则从双字母单词中的两个字母的键中删除值“c”。然后移动到下一个 2 个字母的单词并重复。
这是有错误的片段:
for y in cleanedWords:
if len(y) == 2:
for i in y:
possible[i].remove('c')
这里是错误:
Traceback (most recent call last):
File "F:\python\crypto\cipher.py", line 83, in <module>
possible[i].remove('c')
ValueError: list.remove(x): x not in list
显然我做错了什么。有人能指出我正确的方向吗?我可以不叫“y”吗?
泰勒
请您参考如下方法:
好的,我不熟悉你的(如果你想要详细的答案,请分享它)数据结构,但我认为你需要用以下代码替换你的代码:
for word in (x for x in cleanedWords if len(x) == 2):
for ch in word:
if 'c' in possible[i]:
possible[ch].remove('c')
