python“设置”和“ in”

发布于 2025-01-21 20:30:13 字数 187 浏览 3 评论 0原文

我在python中的设置和操作员有一个小问题。为什么索引是“”集合中的,但不是实际值? 代码如下:

sett = {}

sett[10]="a"
sett[12]="b"
sett[31]="c"

print('a' in sett)
print(31 in sett)

输出: 错误的 真的

I've got a little problem with set and in operator in python.Why is the index "in" the set, but not the actual value?
The code is following:

sett = {}

sett[10]="a"
sett[12]="b"
sett[31]="c"

print('a' in sett)
print(31 in sett)

output:
False
True

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

通知家属抬走 2025-01-28 20:30:14

当使用“ in”用于字典时,它将通过键而不是值搜索。字典是一系列关键:值集。因此,没有“一个”键,但是有一个“ 31”键。如果您尝试:

print('a' in sett[10])

这将返回true,因为现在它正在搜索特定键的值。

When using "in" for dictionaries, it is going to search through the keys, not the the values. The dictionary is a series of key:value sets. So the there is no "a" key, but there is a "31" key. If you try:

print('a' in sett[10])

This will return True because now it is searching the value for a specific key.

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