【发布时间】:2013-12-27 02:55:54
【问题描述】:
我正在尝试编写代码来帮助我完成填字游戏。我遇到以下错误。
1.当我尝试将更大的文本文件与我的单词列表一起使用时,我没有收到任何输出,只有小的 3 字符串单词列表有效。
2.我的测试词列表的前两个字符串的匹配测试是肯定的。我需要它只为我的单词列表中的整个单词测试 true。 [下面代码中的解决方案]
lex.txt 包含
爸爸
添加
测试
我使用以下代码调用代码。
./cross.py 爸爸
[已解决的解决方案]这真的很慢。
#!/usr/bin/env python
import itertools, sys, re
sys.dont_write_bytecode = True
original_string=str(sys.argv[1])
lenth_of_string=len(original_string)
string_to_tuple=tuple(original_string)
with open('wordsEn.txt', 'r') as inF:
for line in inF:
for a in set (itertools.permutations(string_to_tuple, lenth_of_string)):
joined_characters="".join(a)
if re.search('\\b'+joined_characters+'\\b',line):
print joined_characters
【问题讨论】:
-
粘贴您的代码并选择您的代码并点击“ctrl+k”
-
我将下面的“ifjoined_characters in line:”更改为“if re.search('\\b'+joined_characters+'\\b',line):”,这样我就可以准确匹配字符串。
标签: python helper puzzle crossword