0

如果字典类中可用,我只想复制字典

测试文件夹/dict.py

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

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

测试.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 回答