python - 在集合中添加项目(Python)
问题描述
我刚开始学习在集合中添加项目(Python),但后来我不明白为什么会发生这种情况
thisset = {"apple", "banana", "cherry"}
thisset.update("durian", "mango", "orange")
print(thisset)
我得到这样的输出:
{'i', 'o', 'r', 'm', 'cherry', 'n', 'u', 'a', 'apple', 'banana', 'd', 'e', 'g'}
我想要的是将其他 3 个项目放入集合中,我还需要添加/更改什么?
解决方案
根据参考,set.update(*others)
将更新集合,添加所有其他元素,它所做的是set |= other | ...
. 所以在你的情况下,什么thisset.update("durian", "mango", "orange")
是thisset |= set("marian") | set("mango") | set("orange")
. 为了完成你想要的,你需要传递一个列表或一组,比如thisset.update(["durian", "mango", "orange"])
or thisset.update({"durian", "mango", "orange"})
。
推荐阅读
- android - Android Studio 在 osx Big Sur 上的加载速度保持在 80% 左右
- javascript - 试图从表单中检索 cookie 并仅在 javascript 中显示他的信息
- flutter - 先显示输入再模糊密码场效应
- python - Discord.py Intents 使我的机器人不响应任何内容
- java - Spigot 无法将事件 BlockPlaceEvent 传递给插件 v1.0
- c - 如何在 MacOS 上用 C 打印表格?
- prolog - 填充列表(作为数组):没有切割,回溯在给出正确的产生后导致无限递归
- c# - C# UdpClient ReceiveAsync() 性能问题
- mysql - 如何根据条件显示评论
- swift - Swiftui scale效果翻译