python如何遍历元组
467次观看
标签:
遍历
python
老师回答
第一种,使用for in遍历。
girl_tuple = ("貂蝉", "狐狸精","范金链","翠花","小班")
for everyOne in girl_tuple:
print(everyOne)
输出结果:
貂蝉
狐狸精
范金链
翠花
小班
第二种,使用内置函数enumerate遍历。
girl_tuple = ("貂蝉", "狐狸精","范金链","翠花","小班")
for index, everyOne in enumerate(girl_tuple):
print str(index) + everyOne
输出结果:
貂蝉
狐狸精
范金链
翠花
小班
第三种,使用range()或者xrange()内置函数,会把传入的数字,分解成一个List,比如range(),那就是[0,1,2,3,4]。
girl_tuple = ("貂蝉", "狐狸精","范金链","翠花","小班")
for index in range(len(girl_tuple)):
print girl_tuple[index]
girl_tuple = ("貂蝉", "狐狸精","范金链","翠花","小班")
for index in xrange(len(girl_tuple)):
print girl_tuple[index]
输出结果:
貂蝉
狐狸精
范金链
翠花
小班
第四种,使用iter(),同样是内置函数,返回迭代器。
girl_tuple = ("貂蝉", "狐狸精","范金链","翠花","小班")
for everyOne in iter(girl_tuple):
print everyOne
输出结果:
貂蝉
狐狸精
范金链
翠花
小班
python学习网,免费的在线学习python平台,欢迎关注!
©本文版权归环球青藤所有,任何形式转载请联系我们。
免费直播
精选课程
相关推荐