使用以下代码片段,我可以实现所需的内容:
d = {}
d[1] = 'one'
d[2] = 'two'
d[3] = 'three'
exp = ''
for k, v in d.items():
exp += '{}@1 + '.format(v)
exp = exp[:-3]
exp
'one@1 + two@1 + three@1'
我想知道是否有比删除最后一个字符更好的解决方案。
答案 0 :(得分:1)
使用join
:
d = {}
d[1] = 'one'
d[2] = 'two'
d[3] = 'three'
exp = ' + '.join('{}@1'.format(v) for v in d.values())
print(exp)
输出:
one@1 + two@1 + three@1