【发布时间】:2012-11-29 22:23:06
【问题描述】:
我正在尝试运行此代码,以便它为列表的所有元素运行一个函数。此处出于说明目的,基本上应该打印:
'----------Possible Word:', possible_word
对于我列表中的所有项目。所以,如果我输入 ['p', 'r', 's'] 它将运行该打印 3 次,每个项目一个。我的代码在下面——当我运行它时,它只运行 p 和 s,而不是 r,这真的很奇怪。有什么想法吗?
def check_matches(input):
print 'Input:', input
for possible_word in input:
print '----------Possible Word:', possible_word
valid = True
for real_word in word_dictionary:
possible_word_list = list(possible_word)
real_word_list = list(real_word)
print possible_word_list
print real_word_list
number_of_characters_to_check = len(possible_word_list)
for x in range(0, number_of_characters_to_check):
print possible_word_list[x] + real_word_list[x]
if (possible_word_list[x] != real_word_list[x]):
valid = False
if (valid == False):
input.remove(possible_word)
print all_possible
return input
【问题讨论】:
-
这个 .py 文件中还有其他函数和顶部的变量初始化,但我不想发布一个巨大的丑陋块,并认为这就是所有相关的。如果您认为我应该发布其余部分,请直接说。
-
所以我们假设 word_dictionary 是全局列表,并且在之前定义?