我需要一种更快的方法来生成列表的所有排列,然后检查每个排列是否在字典中。
for x in range (max_combo_len, 0, -1):
possible_combos = []
permutations = list(itertools.permutations(bag,x))
for item in permutations:
possible_combos.append(" ".join(item))
#then check to see if each possible combo is in a specific Dict
如果有帮助,列表都将是字符串列表。 ['比如', '这个', '一个']
我的解决方案有效,但速度很慢。可能是我需要停止使用 Python,但我想我应该先让你们这些专家来运行它!
最好的, 加里
最佳答案
一个非常基本的优化:
permutations = list(itertools.permutations(bag,x))
for item in permutations:
可以变成...
for item in itertools.permutations(bag,x):
关于Python - 加快列表排列的生成(以及检查 Dict 中是否排列的过程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3766661/