首页 > 编程语言 >python enumerate

python enumerate

时间:2023-02-26 12:32:21浏览次数:53  
标签:Winter sequence python enumerate start Fall seasons

​enumerate​​(iterablestart=0)

Return an enumerate object. iterable must be a sequence, an ​​iterator​​​, or some other object which supports iteration. The ​​__next__()​​​ method of the iterator returned by ​​enumerate()​​ returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable.

​enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。

​以下是 enumerate() 方法的语法:​

enumerate(sequence, [start=0])

参数

  • sequence -- 一个序列、迭代器或其他支持迭代对象。
  • start -- 下标起始位置的值。

返回值

返回 enumerate(枚举) 对象。

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

Equivalent to:

def enumerate(sequence, start=0):
n = start
for elem in sequence:
yield n, elem
n += 1

​https://www.runoob.com/python/python-func-enumerate.html​

标签:Winter,sequence,python,enumerate,start,Fall,seasons
From: https://blog.51cto.com/u_12074581/6086328

相关文章

  • Python 学习05 函数
    49、函数的定义50、函数的参数51、灵活地使用函数参数......
  • Python笔记--练习题(都来瞧一瞧,看一看嘞)
    利用Python对文件进行操作重新写入的文件如下图所示:统计学生成绩文件的最高分最低分和平均分Python如何统计英文文章出现最多的单词Python统计目录下的文件大小......
  • python计算Friedman排名代码
    python计算Friedman排名代码首先先说输入数据,为了迅速处理,采用csv格式的表格,读者可以先理解这里提供的示例和代码,再自行调整下面是代码,代码会生成一个排名文件Rank.c......
  • Python 元组tuple、 列表list、 字典dict、集合set、迭代器、生成器
    一、元组:tuplePython的元组与列表类似,不同之处在于元组的元素不能修改。元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组tup2=(111,22,33,......
  • python-djanggo 实现读取excel 表格在网页中展示
    1.准备读取数据放到项目文件夹下   2.熟悉表结构    3.准备处理依赖库pipinstall-ihttps://pypi.tuna.tsinghua.edu.cn/simplepandasopenpyxl  ......
  • Python学习记录-异常处理函数的简单使用
    有时一些程序即便语法合规,但在执行过程中也会出现错误,比如下面这个例子defspam(divideBy):return10/divideByprint(spam(10))print(spam(20))print(spam(0))p......
  • Python | 正则表达式(re模块)
    正则表达式使用单个字符串来描述、匹配一系列匹配某个句法规则的字符串re模块是python独有的匹配字符串的模块,该模块种提供功能基于正则表达式实现的,对于字符串进行模糊匹......
  • 用Python画数据分析第三章的图
    importpandasaspdcatering_sale="D:\数据分析\catering_sale.xls"data=pd.read_excel(catering_sale,index_col=u'日期')print(data.describe())importmatplot......
  • Python 正则表达式
    1.常用的元字符1. .匹配任意字符,除换行符\n外 re.search(r'[a-z].*','python\[email protected]')python[a-z]表示小写字母,.表示匹配除换行符外......
  • 数据挖掘python画各类图
    1importpandasaspd2importnumpyasnp3catering_sale='D:\data\catering_fish_congee(1).xls'#餐饮数据4data=pd.read_excel(catering_sale,names=......