-1
所以这是我用Python写的第一个程序。我想要一个字符串,并输出所有真正的单词。我已经完成了(我需要找到一个包含更多单词的参考文件),但它不具有可扩展性,因为如果没有Python花费很长时间才能返回某些内容,我无法输入超过8个字符。如何让这个程序运行得更快?
def lower_and_remove_spaces(fill_string):
'''
function takes a string of 2 or more characters and prints out all the permutations
of words that the characters can make.
'''
lower_string = ''
for i in fill_string:
if i.isalpha():
lower_string += i.lower()
return lower_string
def fill_list(input_string):
iter_list = []
string_list = []
this_string = lower_and_remove_spaces(input_string)
for num in range(2,len(this_string)+1):
iter_list.append(itertools.permutations(this_string,num))
for iters in iter_list:
for lists in iters:
string_list.append(list(lists))
return string_list
def word_list(string):
string_list = fill_list(string)
a_word_list = []
a_string = ''
for i in string_list:
if not a_string == '':
a_word_list.append(a_string)
a_string = ''
for y in i:
a_string += y
return a_word_list
我理解这个跳开了不少,但我不知道什么是更好的方法来做到这一点,以便它的可扩展性?
我有一种感觉,这将是更适合http://codereview.stackexchange.com/。 – 2012-08-13 03:46:13
入口点在哪里? – 2012-08-13 03:50:03
你确实意识到itertools.permutations对于长度为8的东西会给你大约40k个排列。 – 2012-08-13 03:53:46