如何在这里打印钥匙?

发布于 2025-01-11 19:54:26 字数 556 浏览 0 评论 0原文

我给出了一些值&字典中的键&显示了此处存在的两个键的值。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世态炎凉 2025-01-18 19:54:26
  1. 要获取列表作为输出,请使用括号: return [user["id"], user["mail"]]
  2. 您可以尝试 user.keys() 来获取字典键,或 user.items() 来获取键和值:
def save_user_1(**user):
    print('user keys', user.keys())
    for key, value in user.items():
        print("key =", key, ", value =", value)
    return user["id"], user["mail"]

print(save_user_1(id = 1, name = "john", mail = "[email protected]"))

输出:

user keys dict_keys(['id', 'name', 'mail'])
key = id , value = 1
key = name , value = john
key = mail , value = [email protected]
(1, '[email protected]')
  1. To get a list as output, use brackets : return [user["id"], user["mail"]]
  2. You can try user.keys() to get the dictionary keys, or user.items() to get both keys and values :
def save_user_1(**user):
    print('user keys', user.keys())
    for key, value in user.items():
        print("key =", key, ", value =", value)
    return user["id"], user["mail"]

print(save_user_1(id = 1, name = "john", mail = "[email protected]"))

output :

user keys dict_keys(['id', 'name', 'mail'])
key = id , value = 1
key = name , value = john
key = mail , value = [email protected]
(1, '[email protected]')
美人迟暮 2025-01-18 19:54:26
  1. 因为 user["id"], user["mail"] 是一个元组,与 (user["id"], user["mail"]) 相同。
  2. 您正在获取键 'id''mail' 的值。如果您想要密钥(我不确定您为什么想要),您可以返回[k for k in ('id', 'mail') if k in user]
  1. Because user["id"], user["mail"] is a tuple, same as (user["id"], user["mail"]).
  2. You are getting the values for the keys 'id' and 'mail'. If you want the keys (I'm not sure why you would) you could return [k for k in ('id', 'mail') if k in user].
吝吻 2025-01-18 19:54:26

当你返回 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 from print?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文