zoukankan      html  css  js  c++  java
  • 内置函数_枚举与迭代

    枚举与迭代

    • 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))
  • 相关阅读:
    Python中Random随机数返回值方式
    SQL跨库查询
    正则表达式基本语法
    excel VBA使用教程
    使用某些Widows API时,明明包含了该头文件,却报错“error C2065: undeclared identifier”
    电脑开机后数字键盘为关闭状态
    编译Boost 详细步骤 适用 VC6 VS2003 VS2005 VS2008 VS2010
    变量作用域,不能理解,先记下
    解决MySQL 在 Java 检索遇到timestamp空值时报异常的问题
    Annotation
  • 原文地址:https://www.cnblogs.com/zxbdboke/p/10468397.html
Copyright © 2011-2022 走看看