.update()做什么,为什么?
我想了解更多的python,并来到命令“ .update()”。我将其谷歌搜索并确实试图理解,但是它是随机的。
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.update(y)
print(x)
这是我的代码,我可以看到2件事:
- 输出给我5个单词,只有一个人丢失了
- 这些单词被随机选择的,
我做错了吗?为什么只缺少一个单词而不是2/3/4?
I wanted to learn more of python and came to the command ".update()". I googled it up and really tried to understood, however it's randomized.
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.update(y)
print(x)
this is my code and there are 2 things I could see:
- The output gives me 5 words and somehow just one is missing
- The words are getting chosen randomly
Did I do something wrong? Why is there just one word missing and not 2/3/4?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
X
和y
是集合。集合是一种数据结构,仅保留唯一的值。因此,当您更新x
使用y
,x
集将包含两个集合中的唯一值。x
andy
are the sets. Set is a type of data structure that keeps only unique values. So, when you updatex
withy
,x
set will contain unique values from both sets.