2017-06-08 37 views
1

作爲一個初學者,我對這個功能感到非常自豪。雖然我相信可能有做同樣的事情的更簡單,更Python的方式:你可以用單個函數創建字典嗎?

Genes = ['Gen1', 'Gen2', 'Gen3'] 
Mutations = ['Gen1.A', 'Gen1.B', 'Gen2.A', 'Gen3.A', 'Gen3.B', 'Gen3.C'] 

def RawDict(keys, values): 
    dictKeys = [] 
    dictValues = [] 
    for key in keys: 
     keyVal = [] 
     for value in values: 
      if value.find(key) == -1: 
       pass 
      else: 
       keyVal.append(value) 
     dictKeys.append(key) 
     dictValues.append(keyVal)  
    return zip(dictKeys, dictValues) 

GenDict = dict(RawDict(Genes, Mutations)) 

print(GenDict) 

功能上面鍵(基因)內將數值(突變)的,而過於複雜(我認爲)的方式。但是我在想,如果我可以調整這個,所以我可以得到一本詞典只是這樣做:

dict(GenDict, Genes, Mutations) 

print(GenDict) 

我的鬥爭涉及的是,當我使用的功能中字典,這是不行的:

Genes = ['Gen1', 'Gen2', 'Gen3'] 
Mutations = ['Gen1.A', 'Gen1.B', 'Gen2.A', 'Gen3.A', 'Gen3.B', 'Gen3.C'] 

def fullDict(dictName, keys, values): 
    dictKeys = [] 
    dictValues = [] 
    for key in keys: 
     keyVal = [] 
     for value in values: 
      if value.find(key) == -1: 
       pass 
      else: 
       keyVal.append(value) 
     dictKeys.append(key) 
     dictValues.append(keyVal)  
    dictName = dict(RawDict(Genes, Mutations)) 

fullDict(GenDict, Genes, Mutations) 

print(GenDict) 

由於GenDict沒有定義,以上只是不起作用。

+3

你想要的輸出是什麼? – Ajax1234

+0

一個字典,其中基因是關鍵字,突變是每個關鍵字的值。因此Gen1具有['Gen1.A','Gen1.B']作爲值等。 –

+1

爲什麼你希望你的函數看起來像'fullDict(GenDict,Genes,Mutations)'而不是'GenDict = fullDict(基因,突變)'?這似乎很奇怪。 – sietschie

回答

4

從我明白,你想從這個移動:

gen_dict = make_dictionary(genes, mutations) 

這樣:

make_dictionary(gen_dict, genes, mutations) 

其中make_dictionary功能「創建「變量gen_dict

不幸的是,這並不是真正的變量是如何工作的。如果您想要定義一個名爲GenDict的變量,則執行此操作的方法是使用GenDict = ...。你可以做這樣的事情:

gen_dict = {} 
fill_dictionary(gen_dict, genes, mutations) 

這將創建一個名爲gen_dict變量並將其分配給一個新的空字典。那麼你的功能會經歷並添加東西,那本字典:

def fill_dictionary(d, genes, mutations): 
    for g in genes: 
     d[g] = [m for m in mutations if m.startswith(g)] 

但調用的函數不能引起一個新的變量出現在調用者的範圍。 (這並不完全正確,因爲globals(),但對於大多數意圖和目的而言,它是。)

(順便說一句,有一個單線程將創建字典:dictionary = { g : [m for m in mutations if m.startswith(g+".")] for g in genes }。搜索列表解析和在Google或StackOverflow上的詞典解釋 - 它們真是太神奇了!)

+0

這是一個很好的解釋!我在看看:) –

+0

太棒了!現在我已經閱讀了關於列表理解如何工作的一行內容。另外,我想我會濫用字典和列表等,而不是循環大量的變量和字典等。謝謝! :) –

+1

很高興它有幫助!是的,列表解析非常好。 –

4

我假設你想讓「Gen」以它所包含的數值存儲。

Genes = ['Gen1', 'Gen2', 'Gen3'] 
Mutations = ['Gen1.A', 'Gen1.B', 'Gen2.A', 'Gen3.A', 'Gen3.B', 'Gen3.C'] 
the_dict = {i:[] for i in Genes} 

for i in Mutations: 
    new_val = i.split(".") 

    the_dict[new_val[0]].append(i) 

print(the_dict) 

輸出:

{'Gen2': ['Gen2.A'], 'Gen3': ['Gen3.A', 'Gen3.B', 'Gen3.C'], 'Gen1': ['Gen1.A', 'Gen1.B']} 
+1

那。是。美麗。 –

+0

請問你是否知道這些東西的來源?通過在python文檔中查找隨機函數來編寫函數似乎效率不高。從倉庫中獲取函數的方式似乎不是一個好主意...... –

+2

您可能希望先看看Stackoverflow上的其他問題,這些問題用dict-comprehension和list comprehension標記,並嘗試解決其他問題用戶發佈自己的問題。爲了更好地理解list和dict理解,你可以看看這個鏈接:https://stackoverflow.com/questions/14507591/python-dictionary-comprehension – Ajax1234

1

我假設你在Python以外的語言編程方面有一定的背景;一種可讓您更改功能參數的語言。那麼,Python不會。問題不在於使用dict,而是因爲您正在分配給函數參數。這不會影響功能。你想要做的可能是這樣的:

def fullDict(keys, values): 
    return { key: [ value for value in values if key in value] for key in keys } 

print(fullDict(Genes, Mutations)) 
+0

謝謝,這非常有幫助! –

相關問題