所以这是我用 Python 编写的第一批程序之一。我正在尝试获取一个字符串,并输出所有真实单词的字符串。我已经完成了(我需要找到一个包含更多单词的参考文件)但是它不可扩展,因为我不能输入超过 8 个字符,而 Python 需要很长时间才能返回一些东西。
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
我知道这会跳很多,但我想知道有什么更好的方法来做到这一点,以便它具有可扩展性?