Python - 访问集合项目
访问项目
你不能通过引用索引或键来访问集合中的项目。
但是你可以使用 for
循环遍历集合项目,或者使用 in
关键字检查集合中是否存在指定值。
示例
检查 "banana" 是否不存在于集合中
thisset = {"apple", "banana", "cherry"}
print("banana" not in thisset)
自己动手试一试 »
更改项目
集合创建后,你无法更改其项目,但可以添加新项目。
你不能通过引用索引或键来访问集合中的项目。
但是你可以使用 for
循环遍历集合项目,或者使用 in
关键字检查集合中是否存在指定值。
检查 "banana" 是否不存在于集合中
thisset = {"apple", "banana", "cherry"}
print("banana" not in thisset)
自己动手试一试 »
集合创建后,你无法更改其项目,但可以添加新项目。
如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com
如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com