• 内置函数_枚举与迭代


    枚举与迭代

    • enumerate()函数用来枚举可迭代对象中的元素,返回可迭代的enumerate对象,其中每个元素都是包含索引和值的元组,支持start参数,用来指定枚举时的索引起始值

      >>> list(enumerate('abc'))
      [(0, 'a'), (1, 'b'), (2, 'c')]
      >>> list(enumerate(['Python','Java']))
      [(0, 'Python'), (1, 'Java')]
      >>> list(enumerate({'a':97,'b':98,'c':99}.items()))
      [(0, ('a', 97)), (1, ('b', 98)), (2, ('c', 99))]
      >>> for index,value in enumerate(range(10,15)):
      ...   print((index,value),end=' ')
      ...
      (0, 10) (1, 11) (2, 12) (3, 13) (4, 14) >>>
      >>> for item in enumerate(range(5), 6):     # 指定枚举时的索引起始值
      ...   print(item,end=',')
      ...
      (6, 0),(7, 1),(8, 2),(9, 3),(10, 4),>>>
    • iter()函数用来返回指定对象的迭代器,有两种用法

      • iter(iterable)

        要求参数必须为序列或者有自己的迭代器

      • iter(callable,sentinel)

        持续调用参数callable直至其返回sentinel

    • next()函数用来返回可迭代对象中的下一个元素,适用于生成器对象以及zip、enumerate、reversed、map、filter、iter等对象,等价于这些对象的next()方法

      >>> x = [1, 2, 3]
      >>> next(x)                          
      Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
      TypeError: 'list' object is not an iterator
      >>> y = iter(x)                   # 根据列表创建迭代对象
      >>> next(y)
      1
      >>> next(y)
      2
      >>> next(y)
      3
      >>> next(y)
      Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
      StopIteration
      >>> x = range(1,100,3)
      >>> next(x)
      Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
      TypeError: 'range' object is not an iterator
      >>> y = iter(x)
      >>> next(y)
      1
      >>>
      >>> next(y)
      4
      >>> next(y)
      7
      >>> next(y)
      10
      >>> next(y)
      13
      >>> next(y)
      16
      >>> x = {1, 2, 3}
      >>> y = iter(x)
      >>> next(y)
      1
      >>> next(y)
      2
      >>> class T:
      ...   def _ _init_ _(self,seq):
       File "<stdin>", line 2
         def _ _init_ _(self,seq):               ^SyntaxError: invalid syntax>>> class T:...   def __init__(self,seq):...     self.__data=list(seq)...   def __iter__(self):...     return iter(self.__data)...>>> t = T(range(3))>>> next(t)Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: 'T' object is not an iterator>>> ti = iter(t)>>> next(ti)0>>> next(ti)1>>> from queue import Queue   >>> q = Queue() # 创建队列对象>>> for i in range(5):...   q.put(i)...>>> q.put('END')>>> def test():...   return q.get()...>>> for item in iter(test,'END'):...   print(item,end=' ')...0 1 2 3 4 >>>>>> x = map(int,'1234')     # map支持next()函数>>> next(x)1>>> next(x)2>>> x.__next__()3>>> x = reversed('12345678') # reversed支持next()函数>>> for i in range(4):...   print(next(x),end=' ')...8 7 6 5 >>>>>> x = enumerate({'a':97, 'b':98, 'c':99}.items()) # enumerate支持next()函数>>> next(x)(0, ('a', 97))
  • 相关阅读:
    转 linux下vi命令大全
    转 html5 canvas 详细使用教程
    怎么让手机网站自适应设备屏幕宽度? 转自百度经验
    转 :<meta http-equiv="X-UA-Compatible" content="IE=edge" /> 的说明
    转自haorooms :网页防止黑客跨框架攻击,及浏览器安全性想到的
    元信息标记<meta>
    Java语言的主要特性
    学习面向对象的三条主线之一 java类及类的成员
    1.5 MySQL信息源
    1.4在MySQL 8.0中添加,不建议使用或删除的服务器和状态变量及选项
  • 原文地址:https://www.cnblogs.com/zxbdboke/p/10468397.html
Copyright © 2020-2023  润新知