如何在这里打印钥匙?
我给出了一些值&字典中的键&显示了此处存在的两个键的值。
def save_user_1(**user):
return user["id"], user["mail"]
print(save_user_1(id = 1, name = "john", mail = "[email protected]"))
输出: (1, '[电子邮件受保护]')
1.为什么它显示输出为元组 这里?
2.如何在这里获取键的值?(与输出中显示的相反)
I gave some values & keys in a dictionary & showed the value of two keys present here.
def save_user_1(**user):
return user["id"], user["mail"]
print(save_user_1(id = 1, name = "john", mail = "[email protected]"))
Output : (1, '[email protected]')
1.Why does it show the output as a tuple here?
2.How can I get the values of keys here ?(The oppposite of what showed in the output)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
return [user["id"], user["mail"]]
user.keys()
来获取字典键,或 user.items() 来获取键和值:输出:
return [user["id"], user["mail"]]
user.keys()
to get the dictionary keys, oruser.items()
to get both keys and values :output :
user["id"], user["mail"]
是一个元组,与(user["id"], user["mail"])
相同。'id'
和'mail'
的值。如果您想要密钥(我不确定您为什么想要),您可以返回[k for k in ('id', 'mail') if k in user]
。user["id"], user["mail"]
is a tuple, same as(user["id"], user["mail"])
.'id'
and'mail'
. If you want the keys (I'm not sure why you would) you couldreturn [k for k in ('id', 'mail') if k in user]
.当你
返回 user["id"], user["mail"]
时,你就是在告诉 python 返回一个元组。您能否在问题中添加您希望从print
获得什么样的输出?When you do
return user["id"], user["mail"]
you are telling python to return a tuple. Could you add to your question what kind of output you would like fromprint
?