.update()做什么,为什么?

发布于 2025-01-24 04:52:37 字数 298 浏览 2 评论 0原文

我想了解更多的python,并来到命令“ .update()”。我将其谷歌搜索并确实试图理解,但是它是随机的。

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

x.update(y) 

print(x)

这是我的代码,我可以看到2件事:

  1. 输出给我5个单词,只有一个人丢失了
  2. 这些单词被随机选择的,

我做错了吗?为什么只缺少一个单词而不是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:

  1. The output gives me 5 words and somehow just one is missing
  2. The words are getting chosen randomly

Did I do something wrong? Why is there just one word missing and not 2/3/4?

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

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

发布评论

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

评论(1

天荒地未老 2025-01-31 04:52:37

Xy是集合。集合是一种数据结构,仅保留唯一的值。因此,当您更新x使用yx集将包含两个集合中的唯一值。

x and y are the sets. Set is a type of data structure that keeps only unique values. So, when you update x with y, x set will contain unique values from both sets.

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