enumerate() -- Python

#!usr/bin/env python
#coding:utf-8
'''
enumerate()说明:
    1、enumerate()是Python的内置函数;
    2、enumerate字面上是枚举、列举的意思;
    3、对于一个可迭代、可遍历的对象(列表、元组、字符串),enumerate将其组成一个索引序列,利用
        它可以同时获得索引和值;
    4、enumerate多用于for循环中得到计数;
    5、注意:enumerate()返回的是enumerate对象;

'''

'''
需求:给定一串数字01098040234,将非0的数字所处的位置及值进行打印输出;
'''
str1 = '01098040234'
def findNotZero(str):
    return ((index,value) for index,value in enumerate(str) if value!='0')

print(findNotZero(str1))

 

 5、注意:enumerate()返回的是enumerate对象--运行结果如下所示

 

<generator object findNotZero.<locals>.<genexpr> at 0x00000223A8416A98>
[Finished in 0.1s]

 

 

#!usr/bin/env python
#coding:utf-8
'''
enumerate()说明:
    1、enumerate()是Python的内置函数;
    2、enumerate字面上是枚举、列举的意思;
    3、对于一个可迭代、可遍历的对象(列表、元组、字符串),enumerate将其组成一个索引序列,利用
        它可以同时获得索引和值;
    4、enumerate多用于for循环中得到计数;
    5、注意:enumerate()返回的是enumerate对象;

'''

'''
需求:给定一串数字01098040234,将非0的数字所处的位置及值进行打印输出;
'''
str1 = '01098040234'
def findNotZero(str):
    return ((index,value) for index,value in enumerate(str) if value!='0')

print(list(findNotZero(str1)))

 

运行结果:

[(1, '1'), (3, '9'), (4, '8'), (6, '4'), (8, '2'), (9, '3'), (10, '4')]
[Finished in 0.1s]

 

 

注意点:

1、enumerate()指定起始下标enumerate(str,1)

2、列表解析式:[expr for iter_var in iterable if cond_expr]

 

enumerate()的优势:

需求:给你一个列表[‘我’,‘是’,‘寒’,‘岳’];现在想要同时将每个列表元素的索引位置及值同时打印输出;

最直接的做法就是:

for i in range(len(list1)):
    print (i,list1[i])

运行结果:

0 我
123 岳
[Finished in 0.2s]

 

上述方法有些累赘,利用enumerate()会更加直接和优美:

list1 = ['','','','']
for index,value in enumerate(list1):
    print(index,value)

运行结果:

0 我
123 岳
[Finished in 0.1s]

 

转载于:https://www.cnblogs.com/licl11092/p/7450157.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中的enumerate函数是一个内置函数,它可以帮助我们在循环迭代过程中自动生成索引变量。这个函数从Python2.3版本开始被添加到Python中,它返回一个迭代器,每个元素是一个形式为(index,element)的元组,其中index是索引值,element是迭代器中的元素。我们可以在典型的for-in循环中使用数据结构解包功能来充分利用enumerate函数的特性。比如,我们可以这样使用enumerate函数来遍历一个列表: for index, element in enumerate(iterable): # ... 如果我们想验证enumerate函数的运行结果,我们可以将其结果传递给内置函数list(),这样我们可以得到一个包含所有(index,element)元组的列表。比如,如果我们有一个名为names的列表,我们可以这样验证: list(enumerate(names)) 会得到一个包含所有元组的列表,每个元组包含一个索引和一个名字。例如,如果names列表包含了三个元素,分别是'Alice','Bob'和'Carl',那么调用list(enumerate(names))将返回 [(0, 'Alice'), (1, 'Bob'), (2, 'Carl')]。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Pythonenumerate函数](https://blog.csdn.net/sinat_38682860/article/details/109029773)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值