我在 Python 3.3 中解决了这个问题,在我的机器上编译和工作:)
如果你想用一些更复杂的数据(例如:长文本块)来测试它的错误,那里有一堆额外的调试垃圾,比如打印语句。
我使用了 enumerate(),而不是你的 word.index,不确定哪个更 Pythonic?
import sys
### Define variables
test_list = ['i', 'made', 'an', 'ace', 'today', 'at', 'tennis...yeaah', 'booi', ':d']
proc_check = [('made', [-12, 3, 1]),
('ace', [2, 2]),
('today', [-5, -11, -3, 24]),
('tennis...yeaah', [-15, 9, 0, 0, 10, -69, 0, 0, 0, -20, 9, 0, 0]),
('booi', [13, 0, 0])]
final_check = [('ace', [2,2])]
test_list2 = ['ace', 'ace', 'ace']
proc_check2 = [('ace', [2, 2]),
('poo', [3, 3]),
('ace', [2, 2])]
final_check2 = [('ace', [2,2]),('poo', [2,2]),('ace', [2,2])]
### Function definitions
def wordIsEven(word_list, proc_list_check):
final_list = []
procd_list = []
for word in word_list:
temp_list = []
if len(word) >= 3:
for chr1 in word:
if word.index(chr1) != (len(word)-1):
chr2 = word.index(chr1)+1
num = ord(word[chr2]) - ord(chr1)
temp_list.append(num)
temp_tup = (word, temp_list)
procd_list.append(temp_tup)
errors = False
for i, check in enumerate(procd_list):
if check != proc_list_check[i]:
errors = True
print("Word Eval Fail! " + str(check) + " != " + str(proc_list_check[i]))
if errors == True:
print("List compare failed!" )
else:
print("Lists compare equally!")
for tmp_tup in procd_list:
print("Examining Slice: "+str(tmp_tup[1]))
for i, num in enumerate(tmp_tup[1]):
if i + 1 < len(tmp_tup[1]):
num2 = tmp_tup[1][i+1]
if num == num2:
if num != 0:
print("Got one! " + str(tmp_tup))
final_list.append(tmp_tup)
return final_list
### Code execution
my_list = wordIsEven(test_list2, proc_check2)
my_check = final_check2
print("Printing Final list:")
for i, item in enumerate(my_list):
tempStr = str(item)
if item != my_check[i]:
tempStr += " doesn't match check data..." + str(my_check[i])
print(tempStr)
sys.exit()