gpt4 book ai didi

python - 从列表中删除镜像对立面的最快方法

转载 作者:太空宇宙 更新时间:2023-11-04 02:46:16 26 4
gpt4 key购买 nike

假设我有一个元组列表 [(0, 1, 2, 3), (4, 5, 6, 7), (3, 2, 1, 0)],我想删除元组被反转的所有实例,例如从上面的列表中删除 (3, 2, 1, 0)

我目前的(基本)方法是:

L = list(itertools.permutations(np.arange(x), 4))

for ll in L:
if ll[::-1] in L:
L.remove(ll[::-1])

其中花费的时间随着 x 的增加呈指数增长。所以如果 x 很大,这需要很长时间!我怎样才能加快速度?

最佳答案

想到使用 set:

L = set()
for ll in itertools.permutations(np.arange(x), 4):
if ll[::-1] not in L:
L.add(ll)

甚至,为了获得更好的性能:

L = set()
for ll in itertools.permutations(np.arange(x), 4):
if ll not in L:
L.add(ll[::-1])

关于python - 从列表中删除镜像对立面的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45057580/

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