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

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在置換問題中是否可以不使用嵌套循環(huán)?

在置換問題中是否可以不使用嵌套循環(huán)?

我正在編寫一個打印 [a,b,c,d] 的所有排列的代碼。我不想使用遞歸函數(shù),而是使用了 4 個 for 循環(huán),但我的代碼的缺點(diǎn)是循環(huán)有與列表中的元素數(shù)量相同。我的問題是是否可以編寫與元素數(shù)量無關(guān)的代碼。alphabet=["a","b","c","d"]for first in alphabet:    for second in alphabet:        if second != first:            for third in alphabet:                if third!=second and third!=first :                    for fourth in alphabet:                        if fourth!=first and fourth!=second and fourth != third:                                     print(first,second,third,fourth)
查看完整描述

3 回答

?
蕪湖不蕪

TA貢獻(xiàn)1796條經(jīng)驗 獲得超7個贊

這是一個依賴于初始池中沒有重復(fù)項的非遞歸嘗試:


from collections import deque


def perms(pool):

    agenda = deque([([], pool)])

    while agenda:

        perm, left = agenda.popleft()

        if not left:

            yield perm

            # Or, to mimic the original 

            # print(*perm)

        else:

            for x in left:

                agenda.append((perm+[x], [y for y in left if y != x]))


>>> list(perms('abc')))

[['a', 'b', 'c'],

 ['a', 'c', 'b'],

 ['b', 'a', 'c'],

 ['b', 'c', 'a'],

 ['c', 'a', 'b'],

 ['c', 'b', 'a']]


查看完整回答
反對 回復(fù) 2021-09-28
?
哆啦的時光機(jī)

TA貢獻(xiàn)1779條經(jīng)驗 獲得超6個贊

這是一種基本方法(很容易想出)。


先從簡單開始。首先,讓alpha = ["a", "b", "c", "d"]. 首先找到所有以 開頭的排列"a":


    start_a = [["a"]]


    two_start_a = [ start_a[0] + [i]  for i in alpha ]


    to_three_start_a = [ [ j + [i] for i in alpha ] for j in two_start_a ]

    three_start_a = []

    for i in to_three_start_a:

        #cleaned is to exclude list with multiple values

        cleaned = [lst for lst in i if len(set(lst)) == len(lst)] 

        three_start_element += cleaned


    to_four_start_a = [ [ j + [i] for i in alpha ] for j in three_start_a ]

    four_start_a = []

    for i in to_four_start_a:

        #cleaned is to exclude list with multiple values

        cleaned = [lst for lst in i if len(set(lst)) == len(lst)] 

        four_start_element += cleaned

現(xiàn)在我們four_start_a包含所有以 開頭的排列"a"。自動版本是


    start_a = [[alpha[0]]]


    two_start_a = [ start_a[0] + [i]  for i in alpha ]

    k_start_element = two_start_element

    for k in range(3, len(alpha)+1): 

        to_k_start_a = [ [ j + [i] for i in alpha ] for j in k_start_element ]

        k_start_a = []

        for i in to_k_start_a:

            #cleaned is to exclude list with multiple values

            cleaned = [lst for lst in i if len(set(lst)) == len(lst)] 

            k_start_element += cleaned

那么最后的k_start_a就是從 的第一個元素開始的所有排列alpha。


解決方案


所以,對于所有的字母,我們可以自動化如下


all_permutations = []

for element in alpha:


    start_element = [[element]]


    two_start_element = [ start_element[0] + [i]  for i in alpha ]


    k_start_element = two_start_element

    for k in range(3, len(alpha)+1): 

        to_k_start_element = [ [ j + [i] for i in alpha ] for j in k_start_element ]

        k_start_element = []

        for i in to_k_start_element:

            #to exclude list with multiple values

            cleaned = [lst for lst in i if len(set(lst)) == len(lst)] 

            k_start_element += cleaned



    all_permutations.extend( k_start_element )


查看完整回答
反對 回復(fù) 2021-09-28
  • 3 回答
  • 0 關(guān)注
  • 220 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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