Python dict到用户格式字符串

mar*_*npz 1 python string dictionary join

转换Python dict的最简单方法是什么,例如:

a = {'a': 'value', 'b': 'another_value', ...}
Run Code Online (Sandbox Code Playgroud)

使用用户格式的字符串,例如:

'%s - %s\n'
Run Code Online (Sandbox Code Playgroud)

所以它给了我:

a - value
b - another_value
Run Code Online (Sandbox Code Playgroud)

这可行,但也许有更短/更好的使用地图(没有迭代集合)

''.join(['%s %s\n' % o for o in a.items()])
Run Code Online (Sandbox Code Playgroud)

Rom*_*huk 5

我写这个:

>>> print '\n'.join(' '.join(o) for o in a.items())
a value
b another_value
Run Code Online (Sandbox Code Playgroud)

要么:

>>> print '\n'.join(map(' '.join, a.items()))
a value
b another_value
Run Code Online (Sandbox Code Playgroud)