1、tuple
p1 = ('1','2',3)
遍历方法(索引遍历或者for in两种均可):
print "p1[1]", p1[1]
for p in p1:
print p
结果:

2、list
p2 = ['1','2',3]
遍历方法(索引遍历或者for in两种均可):
print "p2[1]", p2[1]
for p in p2:
print p
结果:

3、set
p3 = {'1','2',3}
遍历方法(for in):
for p in p3:
print p
结果:

注:set不支持利用index遍历的方法
本文详细介绍了在Python中如何遍历tuple、list和set三种数据结构,包括使用索引遍历和for-in循环的方式,并提供了具体的代码示例。值得注意的是,set不支持通过索引进行遍历。


被折叠的 条评论
为什么被折叠?



