Python - 加快列表排列的生成(以及检查 Dict 中是否排列的过程)

标签 python list dictionary permutation python-itertools

我需要一种更快的方法来生成列表的所有排列,然后检查每个排列是否在字典中。

        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/

相关文章:

list - "Generating Numbers"拼图

python - 使用/执行列表中的字符串作为对象名称(python)

python - 按数值排序字典

Python 脚本从列表中删除唯一元素并按正确顺序打印重复元素的列表

ios - NSUserDefaults 中保存的 NSDictionary 不是最新的

python - 将 html 实体转换为 python 中的值

python - 我想在 pygame 中进行可视化的冒泡排序,排序有效,但可视化不起作用

python - 在 PyGame 中移动图像会隐藏所有其他图像

python - 如何将字典的动态值写入csv python

python - 更新字典的多维字典