我有以下代码:
ex_dict={1:"how",3:"do you",7:"dotoday"}
for key in ex_dict:
string= key," and this is ", ex_dict[key]
print (string)
Run Code Online (Sandbox Code Playgroud)
输出是:
(1, ' and this is ', 'how')
(3, ' and this is ', 'do you')
(7, ' and this is ', 'dotoday')
Run Code Online (Sandbox Code Playgroud)
我的预期输出:
1 and this is how
3 and this is do you
7 and this is dotoday
Run Code Online (Sandbox Code Playgroud)
我似乎无法弄清楚如何摆脱输出中的字典格式.
只是+
用来组合字符串:
string = str(key) + " and this is " + ex_dict[key]
Run Code Online (Sandbox Code Playgroud)
由于key
是一个整数,并且您只能使用+
运算符连接字符串,因此您也应该转换key
为字符串.
归档时间: |
|
查看次数: |
63 次 |
最近记录: |