我有一个字典,上面写着“可能”的列表,上面有'a' - 'z'键。每个键都有一个列表的值,其中包含“a”到“z”。基本上,26个字母。
我将字符串清理成小写并删除标点并将每个单词保存到名为“cleaningWords”的列表中。
我想查看列表,如果列表中的单词只有两个字母,请从键中删除两个字母单词中两个字母的值“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”我怎么做?
泰勒
答案 0 :(得分:1)
好的,我不熟悉你的(如果你想要详细的答案,请分享它)数据结构,但我认为你需要用以下代码替换你的代码:
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')
答案 1 :(得分:0)
for y in cleanedWords:
if len(y) == 2:
print y
for i in y:
try:
possible[i].remove('c')
except ValueError:
pass