1

我有一本与此相似的字典:

mydic = {'key1':['va1','va2'], 'key2':['vb1','vb2']}

我想打印出这些文件路径,例如

/path/to/dir/key1/dir2/va1
/path/to/dir/key1/dir2/va2
/path/to/dir/key2/dir2/vb1
/path/to/dir/key3/dir2/vb2

我努力了,

for k, v in mydic.iteritems():
    print "/path/to/dir/k/dir2/v"

但这一个打印v为列表。我怎样才能实现上述目标?谢谢...

4

1 回答 1

2

您可以使用os.path.join(),如下所示:

import os

mydic = {'key1':['va1','va2'], 'key2':['vb1','vb2']}


BASE_DIR = os.path.join('path', 'to', 'dir')
ADD_DIR = 'dir2'
for k, v in mydic.iteritems():
    for item in v:
        print os.path.join(BASE_DIR, k, ADD_DIR, item)

印刷:

/path/to/dir/key2/dir2/vb1
/path/to/dir/key2/dir2/vb2
/path/to/dir/key1/dir2/va1
/path/to/dir/key1/dir2/va2
于 2013-08-26T23:03:48.093 回答