您的位置:首页 > 编程语言 > Python开发

【脚本语言系列】关于Python基础知识枚举,你需要知道的事

2017-07-19 10:18 936 查看

如何使用枚举

遍历函数

# -*- coding:utf-8 -*-
for counter, value in enumerate(some_list):
print(counter, value)


# -*- coding:utf-8 -*-
the_list = ['red', 'yellow','green']
for counter, value in enumerate(the_list, 1):
print counter, value

print "\n"

for counter, value in enumerate(the_list, 2):
print counter, value


创建具有索引的元组

# -*-  coding:utf-8 -*-
the_list = ['red', 'yellow', 'green']
indexed_list = list(enumerate(the_list, 1))
print indexed_list


[(1, 'red'), (2, 'yellow'), (3, 'green')]


什么是枚举

枚举(eumerate)是Python内置函数,允许遍历数据并且自动计数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  脚本语言 python 枚举
相关文章推荐