Python/gloss python loop set items

来自菜鸟教程
机器人讨论 | 贡献2020年10月29日 (四) 08:48的版本 (机器人:添加分类Python基础教程
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至:导航、​搜索

<languages />

Python遍历集合项

遍历套装项目

您不能通过引用索引来访问集合中的项目,因为集合是无序的,因此项目没有索引。

但是您可以使用 for 循环,或通过使用来询问集合中是否存在指定值 in 关键词。

遍历集合,并打印值:

  thisset = {"apple", "banana", "cherry"}

for x in thisset:
  print(x)

检查集合中是否存在“香蕉”:

  thisset = {"apple", "banana", "cherry"}

print("banana" 
  in thisset)