python循环for不从零开始_Python-多处理-巨大的for循环

博客探讨了在Python中处理大量循环的优化方法,特别是针对一个需要迭代440个元素列表的问题。作者提出使用多处理来加速计算,并提供了一个解决方案,通过改进的算法和`itertools`模块来避免指数级的计算复杂性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下午好,

我对Python还是很陌生,我必须解决一个需要尝试数十亿个假设的问题...更具体地说,我需要迭代440个元素的列表,但我需要这样做8次...(是的,我知道OS迭代的次数完全是疯狂的。

我的机器相当不错,所以我想使用多处理python功能来加快速度。

您是否知道任何简单的解决方案可以从我的机器的处理能力中获利?

输入:

配对对:

for ind1 in range(16,37):

for ind2 in range(16,37):

ListPairsAux = []

ListPairsAux.append(ind1)

ListPairsAux.append(ind2)

ListPairs.append(ListPairsAux)

为了简化问题,您可以假设len(list1 [i])和len(list2 [i])都是整数,并且都等于198。(在实际问题中,我们实际上有21个不同的整数,但是都以相同的顺序排列-这意味着它们不会超出198。

for循环如下:

for first in ListPairs:

print(str(first))

for second in ListPairs:

for third in ListPairs:

for fourth in ListPairs:

for fifth in ListPairs:

for sixth in ListPairs:

for seventh in ListPairs:

sumA = first[0] + second[0] + third[0] + fourth[0] + fifth[0] + sixth[0] + seventh[0]

sumB = first[1] + second[1] + third[1] + fourth[1] + fifth[1] + sixth[1] + seventh[1]

for i in range(len(list1)):

if sumA == len(list1[i]) and sumB == len(list2[i]):

List7 = []

List7 = [first, second, third, fourth, fifth, sixth, seventh]

ListsOut[i].append(List7)

for eighth in ListPairs:

sumA = first[0] + second[0] + third[0] + fourth[0] + fifth[0] + sixth[0] + seventh[0] + eighth[0]

sumB = first[1] + second[1] + third[1] + fourth[1] + fifth[1] + sixth[1] + seventh[1] + eighth[1]

for i in range(len(list1)):

if sumA == len(list1[i]) and sumB == len(list2[i]):

List8 = []

List8 = [first, second, third, fourth, fifth, sixth, seventh, eighth]

ListsOut[i].append(List8)

非常感谢!

解决方案

您发布的解决方案可能永远不会完成,因为它将需要经历超过10 ^ 21个元素的组合。与其使用多重处理,不如使用更快的算法。

使用您在问题中使用的list1,list2和lists_out,我们正在寻找组合16和36之间的整数的方法,以使它们求和成为list1和list2中序列的长度。组合应为[16,36]范围内的7或8个整数。

import itertools

def so43965562(list1, list2, lists_out, lower=16, upper=36):

assert len(list1) == len(list2) == len(lists_out)

for n in (7, 8):

for i in range(len(list1)):

# Find all combinations of n numbers in [lower, upper]

# that sum to len(list1[i])

combs1 = combinations_summing_to(lower, upper, n, len(list1[i]))

# Find all combinations of n numbers in [lower, upper]

# that sum to len(list2[i])

combs2 = combinations_summing_to(lower, upper, n, len(list2[i]))

for t1, t2 in itertools.product(combs1, combs2):

result = [(v1, v2) for v1, v2 in zip(t1, t2)]

lists_out[i].append(result)

以下函数写s为和n之间的整数之l和u。

def combinations_summing_to(l, u, n, s, suffix=()):

"""In which ways can s be written as the sum of n integers in [l, u]?

>>> # Write 2 as a sum of 4 integers between 0 and 5.

>>> print(list(combinations_summing_to(0, 5, 4, 2)))

[(0, 0, 0, 2), (0, 0, 1, 1)]

>>> # Write 5 as a sum of 3 integers between 0 and 5.

>>> print(list(combinations_summing_to(0, 5, 3, 5)))

[(0, 0, 5), (0, 1, 4), (0, 2, 3), (1, 1, 3), (1, 2, 2)]

>>> # Write 12 as a sum of 3 integers between 0 and 5.

>>> print(list(combinations_summing_to(0, 5, 3, 12)))

[(2, 5, 5), (3, 4, 5), (4, 4, 4)]

>>> # Write 34 as a sum of 2 integers between 16 and 36.

>>> print(list(combinations_summing_to(16, 36, 2, 34)))

[(16, 18), (17, 17)]

"""

if n == 0:

return (suffix,) if s == 0 else ()

elif n == 1:

return ((s,) + suffix,) if l <= s <= u else ()

else:

return itertools.chain.from_iterable(

# Combinations summing to s where the last element is k

combinations_summing_to(l, k, n - 1, s - k, (k,) + suffix)

for k in range(u, l-1, -1)

# Early bailout if you can't make s with all elements <= k

if l * n <= s <= k * n)

您可以按以下方式运行解决方案:

lists_out = [[]]

so43965562(list1=[[0]*(7*16+1)], list2=[[0]*(7*16+2)], lists_out=lists_out)

for result in lists_out[0]:

print(result)

# Outputs the following two combinations:

# [(16, 16), (16, 16), (16, 16), (16, 16), (16, 16), (16, 16), (17, 18)]

# [(16, 16), (16, 16), (16, 16), (16, 16), (16, 16), (16, 17), (17, 17)]

lists_out = [[]]

n = 133

so43965562(list1=[[0]*n], list2=[[0]*n], lists_out=lists_out)

print(len(lists_out[0]))

# Outputs 1795769, takes about 2.5 seconds to run.

请注意,输出大小呈指数增长,当n = 7 * 16 = 112时从零开始,因此当您编写问题时,当n = 198时,计算所有组合仍将花费很长时间。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值