我有一个包含许多替换字段的长字符串,然后用以下格式进行格式化:
firstRep = replacementDict['firstRep']
secondRep = replacementDict['secondRep']
.
.
.
nthRep = replacementDict['nthRep']
newString = oldString.format(firstRep = firstRep,
secondRep = secondRep,...,
nthRep = nthRep)
有没有一种方法可以避免必须单独设置每个选项并使用循环方法?在
谢谢。在
您可以unpack argument dicts with a ^{} prefix :
使用^{} ,它是为这个用例提供的
注意:
format_map
仅在python3.2+中提供。在python2中,您可以使用**
解包(参见this相关问题),hower这会迫使python复制字典,因此速度会慢一点,并使用更多内存。在你可以这样简单地解包字典
引用Format Examples
^{pr2}$相关问题 更多 >
编程相关推荐