0

辞書クラスで利用可能な場合は、辞書をコピーしたいだけです

テストフォルダー/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
4

2 に答える 2

3

どうですか:

c = getattr(dictRef, 'two')
于 2013-02-18T07:07:14.660 に答える
0

または、次のことを試すことができます。

if dictRef.has_key(key): 

これがあなたが探しているものだと思います!

于 2013-02-18T09:16:15.433 に答える