日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 默认站点!

默认站点

當(dāng)前位置: 首頁 >

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

發(fā)布時(shí)間:2023/12/1 37 豆豆
默认站点 收集整理的這篇文章主要介紹了 python循环for不从零开始_Python-多处理-巨大的for循环 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

下午好,

我對(duì)Python還是很陌生,我必須解決一個(gè)需要嘗試數(shù)十億個(gè)假設(shè)的問題...更具體地說,我需要迭代440個(gè)元素的列表,但我需要這樣做8次...(是的,我知道OS迭代的次數(shù)完全是瘋狂的。

我的機(jī)器相當(dāng)不錯(cuò),所以我想使用多處理python功能來加快速度。

您是否知道任何簡單的解決方案可以從我的機(jī)器的處理能力中獲利?

輸入:

配對(duì)對(duì):

for ind1 in range(16,37):

for ind2 in range(16,37):

ListPairsAux = []

ListPairsAux.append(ind1)

ListPairsAux.append(ind2)

ListPairs.append(ListPairsAux)

為了簡化問題,您可以假設(shè)len(list1 [i])和len(list2 [i])都是整數(shù),并且都等于198。(在實(shí)際問題中,我們實(shí)際上有21個(gè)不同的整數(shù),但是都以相同的順序排列-這意味著它們不會(huì)超出198。

for循環(huán)如下:

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)

非常感謝!

解決方案

您發(fā)布的解決方案可能永遠(yuǎn)不會(huì)完成,因?yàn)樗鼘⑿枰?jīng)歷超過10 ^ 21個(gè)元素的組合。與其使用多重處理,不如使用更快的算法。

使用您在問題中使用的list1,list2和lists_out,我們正在尋找組合16和36之間的整數(shù)的方法,以使它們求和成為list1和list2中序列的長度。組合應(yīng)為[16,36]范圍內(nèi)的7或8個(gè)整數(shù)。

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)

以下函數(shù)寫s為和n之間的整數(shù)之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)

您可以按以下方式運(yù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.

請(qǐng)注意,輸出大小呈指數(shù)增長,當(dāng)n = 7 * 16 = 112時(shí)從零開始,因此當(dāng)您編寫問題時(shí),當(dāng)n = 198時(shí),計(jì)算所有組合仍將花費(fèi)很長時(shí)間。

總結(jié)

以上是默认站点為你收集整理的python循环for不从零开始_Python-多处理-巨大的for循环的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得默认站点網(wǎng)站內(nèi)容還不錯(cuò),歡迎將默认站点推薦給好友。