“Python/gloss python loop set items”的版本间差异

来自菜鸟教程
跳转至:导航、​搜索
(Pywikibot 4.4.0.dev0)
 
(机器人:添加分类Python基础教程
 
第58行: 第58行:
 
</div>
 
</div>
 
<br />
 
<br />
 +
 +
[[分类:Python基础教程]]

2020年10月29日 (四) 08:48的最新版本

<languages />

Python遍历集合项

遍历套装项目

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

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

遍历集合,并打印值:

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

for x in thisset:
  print(x)

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

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

print("banana" 
  in thisset)