我有以下代码:
ex_dict={1:"how",3:"do you",7:"dotoday"}
for key in ex_dict:
string= key," and this is ", ex_dict[key]
print (string)
输出为:
(1, ' and this is ', 'how')
(3, ' and this is ', 'do you')
(7, ' and this is ', 'dotoday')
我的预期输出:
1 and this is how
3 and this is do you
7 and this is dotoday
我似乎不知道如何去掉输出中的字典格式。你知道吗
你看的不是字典格式,而是tuples。你知道吗
改用字符串格式:
此外:您可以使用
最后:预先警告python中的字典有arbitrary order.如果您希望排序始终相同,那么使用collections.OrderedDict更安全。因为否则,您将依赖于实现细节。想亲眼看看吗?使
ex_dict = {1:"how",3:"do you",7:"dotoday", 9:"hi"}
。你知道吗替换
与
在python中,用+而不是
,用于元组
只需使用
+
组合字符串:由于
key
是一个整数,并且只能使用+
运算符连接字符串,因此也应该将key
转换为字符串。你知道吗相关问题 更多 >
编程相关推荐