gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:14:08 25 4
gpt4 key购买 nike

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

        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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com