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

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

具有唯一值的排列

具有唯一值的排列

慕勒3428872 2019-06-23 17:19:55
具有唯一值的排列排列根據(jù)元素的位置而不是其值來(lái)生成元素被視為唯一的位置。所以基本上我想避免這樣的重復(fù):>>> list(itertools.permutations([1, 1, 1]))[(1, 1, 1), (1, 1, 1), (1, 1, 1), (1, 1, 1), (1, 1, 1), (1, 1, 1)]事后過濾是不可能的,因?yàn)樵谖业那闆r下排列的數(shù)量太大。有誰(shuí)知道一個(gè)合適的算法嗎?非常感謝!編輯:我主要想要的是:x = itertools.product((0, 1, 'x'), repeat=X)x = sorted(x, key=functools.partial(count_elements, elem='x'))這是不可能的,因?yàn)閟orted創(chuàng)建一個(gè)列表,則itertools.Products的輸出太大。對(duì)不起,我應(yīng)該描述一下實(shí)際的問題。
查看完整描述

3 回答

?
陪伴而非守候

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

class unique_element:
    def __init__(self,value,occurrences):
        self.value = value
        self.occurrences = occurrencesdef perm_unique(elements):
    eset=set(elements)
    listunique = [unique_element(i,elements.count(i)) for i in eset]
    u=len(elements)
    return perm_unique_helper(listunique,[0]*u,u-1)def perm_unique_helper(listunique,result_list,d):
    if d < 0:
        yield tuple(result_list)
    else:
        for i in listunique:
            if i.occurrences > 0:
                result_list[d]=i.value
                i.occurrences-=1
                for g in  perm_unique_helper(listunique,result_list,d-1):
                    yield g
                i.occurrences+=1a = list(perm_unique([1,1,2]))print(a)

結(jié)果:

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

編輯(這是如何工作的):

我重寫了上層程序,使其更長(zhǎng),但更易讀。

我通常很難解釋某事是如何工作的,但讓我試試。為了理解這是如何工作的,你必須理解類似的,但一個(gè)更簡(jiǎn)單的程序,將產(chǎn)生所有的排列與重復(fù)。

def permutations_with_replacement(elements,n):
    return permutations_helper(elements,[0]*n,n-1)#this is generatordef permutations_helper(elements,result_list,d):
    if d<0:
        yield tuple(result_list)
    else:
        for i in elements:
            result_list[d]=i
            all_permutations = permutations_helper(elements,result_list,d-1)#this is generator            
            for g in all_permutations:
                yield g

這個(gè)程序顯然要簡(jiǎn)單得多:d表示置換_助手中的深度,并有兩個(gè)函數(shù)。一個(gè)函數(shù)是遞歸算法的停止條件,另一個(gè)函數(shù)是傳遞給我們的結(jié)果列表。

我們沒有返回每個(gè)結(jié)果,而是生成結(jié)果。如果沒有函數(shù)/運(yùn)算符yield我們不得不把結(jié)果推到停車點(diǎn)排隊(duì)。但是這樣,一旦停止條件滿足,結(jié)果就通過所有的疊加傳播給調(diào)用者。這就是.的目的
for g in  perm_unique_helper(listunique,result_list,d-1): yield g因此,每個(gè)結(jié)果都被傳播到調(diào)用方。

回到原來(lái)的程序:我們有一個(gè)獨(dú)特的元素列表。在使用每個(gè)元素之前,我們必須檢查有多少元素仍然可用,才能將其推送到結(jié)果列表中。這個(gè)程序的工作與permutations_with_replacement不同之處在于,每個(gè)元素不能重復(fù)更多次,即PER_UNIQUE_HELLER中的元素。


查看完整回答
反對(duì) 回復(fù) 2019-06-23
?
胡子哥哥

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

這依賴于實(shí)現(xiàn)細(xì)節(jié),即排序迭代的任何排列都是按排序順序排列的,除非它們是先前排列的重復(fù)。

from itertools import permutationsdef unique_permutations(iterable, r=None):
    previous = tuple()
    for p in permutations(sorted(iterable), r):
        if p > previous:
            previous = p            yield pfor p in unique_permutations('cabcab', 2):
    print p

施予

('a', 'a')('a', 'b')('a', 'c')('b', 'a')('b', 'b')('b', 'c')('c', 'a')('c', 'b')('c', 'c')


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

添加回答

舉報(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)