在自己内部引用字典

AK4*_*K47 5 python dictionary

是否可以从同一个字典中的字典条目中获取值?我想建立一个目录列表,同时引用以前添加的目录..

common_dirs = {
    'root': '/var/tmp',
    'java_path': os.path.join(dict.get('root'), 'java'),
    'application_path': os.path.join(dict.get('java_path'), 'my_app')
}
Run Code Online (Sandbox Code Playgroud)

Mos*_*oye 8

为什么不更新字典:

my_dict = {'root': '/var/tmp'}
my_dict.update({'file': os.path.join(my_dict.get('root'), 'file')})
Run Code Online (Sandbox Code Playgroud)

不要dict用作名字.您可能需要以后使用真正的dict内置功能.

  • 为什么甚至使用更新?只要做`my_dict ['file'] = os.path.join(my_dict.get('root'),'file')` (5认同)
  • @MorganThrapp这是一样的.只需要使用尽可能多的`{}`来匹配OP的问题:) (2认同)