2013-02-18 48 views
0

我只是想複製如果有一個在字典詞典類詞典副本,如果字典可用

testfolder/dict.py

one={ 
    "one1":"one1Data", 
    "one2":"one2Data", 
    "one2":"one3Data"} 

    two={ 
    "two1":"two1Data", 
    "two2":"two2Data", 
    "two2":"two3Data"} 

test.py

import testfolder.dict as dictRef 

    print dictRef.two # it prints the dictionary) 

但我想要做的是複製字典,如果可用可在dictRef

a='two' 
    if hasattr(dictRef, a): # this will not work but searching some alternate way to do.. 
    c = dictRef.a # Jus trying to achieve this 
    print C# should print dictRef.two 
+0

一些混淆可能是'詞典'是一個模塊,而不是字典的結果。 – orip 2013-02-18 10:05:50

回答

3

如何:

c = getattr(dictRef, 'two') 
0

或者你可以嘗試:

if dictRef.has_key(key): 

我認爲這是你在找什麼!