2014-01-21 59 views
0

目前,我有,我想創建鍵和值的最終總榜單的字典創建值總榜單:如何從字典

adict = {'f': {'g', 'd'}, 
     'd': {'g'}, 
     'e': {'d'}, 
     'b': {'d'}, 
     'c': {'f', 'e'}, 
     'a': {'b', 'c'}} 

目前我在這個尋找功能格式:

def create_final_total_list(thedictionary: dict(), startingkey:str): 
    final_list = [] 
    # function here 

我想要的是讓用戶輸入一個開始鍵,它將鍵和它的值附加到final_list。而且這些值也會成爲鍵值,這些鍵值也會將其所有值添加到final_list中等等。如果啓動鍵將是「一」,那麼它首先確實

實施例:

final_list = ['a', 'b', 'c'] 

然後,它會看到「b」和「c」的值,並從字典添加它們的值,從而它將成爲:

final_list = ['a', 'b', 'c', 
       'b', 'd', 
       'c', 'f', 'e', ...] 

而且從價值觀 'd', 'f' 和 'E' 它會成爲:

final_list = ['a', 'b', 'c', 

       'b', 'd', 
       'c', 'f', 'e' 

       'd', 'g' 
       'f', 'g', 'd' 
       'e', 'd' ...] 

等等...

它有點像達到功能,從一個鍵和從其值到達下一個。

我將如何在Python 3.3中解決這個問題?

+0

我正確的假設輸入字典通過其相鄰節點定義了一些定向圖嗎? – bereal

+0

這是拓撲排序,我想。 –

+0

@bereal你的假設是正確的。 – user2559679

回答

1

如何如下:

使用deque爲您的隊列:

>>> from topsort import adict, create_final_total_list 
>>> adict 
{'a': {'b', 'c'}, 'b': {'d'}, 'c': {'e', 'f'}, 'd': {'g'}, 'e': {'d'}, 'f': {'d', 'g'}} 
>>> create_final_total_list(adict, 'c') 
['c', 'e', 'f', 'f', 'd', 'g', 'g', 'd', 'g', 'g', 'e', 'd', 'd', 'g', 'g'] 

該函數的代碼:

def create_final_total_list(the_dict, startingkey): 
    final_list = [] 
    var = deque([startingkey]) 
    while var: 
     u = var.pop() 
     final_list.append(u) 
     s = the_dict[u] if u in the_dict else None 
     if s: 
      final_list.extend(s) 
      var.extend(s) 

    return final_list 
0

我修改你的第一部字典,使其包含的字典的列表(其他語法不正確),那麼我只是通過最終列表作爲輸出參數:

>>> adict = {'f': ['g', 'd'], 
     'd': ['g'], 
     'e': ['d'], 
     'b': ['d'], 
     'c': ['f', 'e'], 
     'a': ['b', 'c']} 
>>> def create_final_total_list(thedictionary, startingkey, final_list): 
    final_list.append(startingkey) 
    final_list.extend(thedictionary[startingkey]) 


>>> fl = [] 
>>> create_final_total_list(adict, 'a', fl) 
>>> fl 
['a', 'b', 'c'] 
>>> create_final_total_list(adict, 'b', fl) 
>>> create_final_total_list(adict, 'c', fl) 
>>> fl 
['a', 'b', 'c', 'b', 'd', 'c', 'f', 'e'] 
>>> 
+0

'{'g','d'}'是有效的語法並定義了一個'set' –

+0

哦,對了,謝謝你指出這個! – Emmanuel

0

我看到一個發佈的答案,所以我會以任何方式發佈我的。我的隊列執行是簡單的列表,雖然不知道是否會有使用隊列或dqueue任何好處

實施

def create_final_total_list(thedictionary, key): 
    final_list = list(thedictionary[key]) 
    Q = list(thedictionary[key]) 
    while Q: 
     key = Q.pop(0) 
     final_list+=thedictionary.get(key, []) 
     Q+=thedictionary.get(key, []) 
    return final_list 

輸出

>>> create_final_total_list(adict, 'a') 
['c', 'b', 'e', 'f', 'd', 'd', 'd', 'g', 'g', 'g', 'g'] 
0

的遞歸實現可能是:

from itertools import chain 

adict = {'f': {'g', 'd'}, 
     'd': {'g'}, 
     'e': {'d'}, 
     'b': {'d'}, 
     'c': {'f', 'e'}, 
     'a': {'b', 'c'}} 

def create_final_list(dict_, key): 
    values = list(dict_.get(key, [])) 
    return [key] + values + \ 
     list(chain(*[create_final_list(dict_, v) for v in values])) 

print create_final_list(adict, "a") 

此打印:

['a', 'c', 'b', 'c', 'e', 'f', 'e', 'd', 'd', 'g', 'g', 'f', 'd', 'g', 'd', 'g', 'g', 'g', 'b', 'd', 'd', 'g', 'g'] 

請注意,元件的一組{"a","b"}的順序不是固定的,從而順序可以變化。