第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何將較大的數(shù)組分布在較小的數(shù)組上

如何將較大的數(shù)組分布在較小的數(shù)組上

catspeake 2023-01-04 13:34:50
我的問(wèn)題有點(diǎn)復(fù)雜,但是這個(gè)問(wèn)題可以用一個(gè)例子來(lái)寫得相當(dāng)籠統(tǒng):我有一個(gè)池列表(pools)需要有一個(gè)子列表(children)均勻分布在列表中pools。該children列表已經(jīng)排序,因此可以安全地假設(shè)它可以按pools當(dāng)前順序分布在 上。例如,如果我有并且[pool1, pool2]我[child1, child2, child3]希望pool1被分配child1并且將被分配:child3pool2child2pools = ['pool1', 'pool2']children = ['child1', 'child2', 'child3']def print_assignment(pool, child)  print('{} assigned to {}'.format(child, pool)# The expectation is that distribute would perform the core logic and # call print_assignment during each assignmentdistribute(pools, children, print_assignment)預(yù)期輸出為:child1 assigned to pool1child2 assigned to pool2child3 assigned to pool1期望pools和的數(shù)量children可以是任意大小,但是,以下情況始終為真:len(pools) < len(children)。
查看完整描述

3 回答

?
守著一只汪

TA貢獻(xiàn)1872條經(jīng)驗(yàn) 獲得超4個(gè)贊

from itertools import cycle


pools = ['pool1', 'pool2']

children = ['child1', 'child2', 'child3']


c = cycle(pools)

for child in children:

? ? print('{} assigned to {}'.format(child, next(c)))

印刷:


child1 assigned to pool1

child2 assigned to pool2

child3 assigned to pool1


查看完整回答
反對(duì) 回復(fù) 2023-01-04
?
瀟瀟雨雨

TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個(gè)贊

我認(rèn)為它更具可讀性:


from itertools import cycle


pools = ['pool1', 'pool2']

children = ['child1', 'child2', 'child3']


for child, pool in zip(children, cycle(pools)):

? ? print(f'{child} assigned to {pool}')

輸出:


child1 assigned to pool1

child2 assigned to pool2

child3 assigned to pool1


查看完整回答
反對(duì) 回復(fù) 2023-01-04
?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

你可以這樣做:


for elem in children:

    if children.index(elem) % 2 == 0:

        print(f"{elem} to {pools[0]}")

    else:

        print(f"{elem} to {pools[1]}")

考慮到你只有兩個(gè)池,如果他的索引是奇數(shù),你可以將孩子分配給 pool1。


查看完整回答
反對(duì) 回復(fù) 2023-01-04
  • 3 回答
  • 0 關(guān)注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)