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如何遍历元组 的全部内容, 来源链接: utcz.com/z/525188.html