ITPub博客

首页 > IT职业 > IT生活 > python如何遍历集合?

python如何遍历集合?

IT生活 作者:pinja 时间:2021-09-11 10:16:42 0 删除 编辑

python中遍历集合的方法:

1、使用for循环遍历

A = {'1','2','star'}
for item in A:
    print(item,end='')

Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。

2、使用while循环遍历

## 用while循环遍历
A = {'1','2','star'}
try:
    while True:
        print(A.pop(),end='')
except:
    pass

Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。

更多Python知识请关注Python自学网。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2637/viewspace-2833633/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-16

  • 博文量
    112
  • 访问量
    1406