gpt4 book ai didi

python - 从列表列表中获取所有唯一组合,直到第 n 个组合

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:41:59 24 4
gpt4 key购买 nike

我有一个列表列表,其中每个内部列表中的变量是图像的路径。通常每个内部列表的长度约为 35,并且在一个列表中将有 9 个这样的列表。例如

 [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]]

我想从这个列表列表中生成 9 的唯一组合。如果我使用 itertools.product 它可以工作,但花费的时间太长 - 它会使我的计算机崩溃。我需要的是能够一直上升到第 n 个组合的东西,其中 n 可能约为 200。我试过这个...

  list(itertools.product(*z))[:200]

其中 z 是我的列表列表,但它不起作用,因为它在执行切片之前先生成所有组合(太慢了)。

还有其他有效的方法来运行它吗?

编辑:我应该补充一点,我需要将其转换为列表列表...

基准测试:

布拉德:

 def combos():
my_iter = itertools.product(*z)
print([next(my_iter) for i in range(1, 10000)]

cProfile.run('print(combos())')
10006 function calls in 0.021 seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.021 0.021 <string>:1(<module>)
1 0.000 0.000 0.021 0.021 gcm.py:17(combos)
1 0.002 0.002 0.004 0.004 gcm.py:19(<listcomp>)
1 0.000 0.000 0.021 0.021 {built-in method builtins.exec}
9999 0.003 0.000 0.003 0.000 {built-in method builtins.next}
2 0.016 0.008 0.016 0.008 {built-in method builtins.print}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}

DeepSpaces 更新的答案:

 cProfile.run('print(list(my_gen(z, 10000)))')

10005 function calls in 0.019 seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)
1 0.001 0.001 0.019 0.019 <string>:1(<module>)
10001 0.004 0.000 0.004 0.000 gcm.py:10(my_gen)
1 0.000 0.000 0.019 0.019 {built-in method builtins.exec}
1 0.014 0.014 0.014 0.014 {built-in method builtins.print}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}

为了迈出这一步,我做了这个......

   def my_gen(z, limit):
count = 0
for i in itertools.product(*z):
if count < limit:
if count % 1000 == 0:
yield i
count += 1
else:
count += 1
continue
else:
raise StopIteration

最佳答案

试试这个:

my_iter = itertools.product(*z)
[next(my_iter) for i in range(200)]

关于python - 从列表列表中获取所有唯一组合,直到第 n 个组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47183705/

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