python内置方法、模块讲解(十七)

  • 2016-04-03 12:00:43
  • 1159
  • 0

四十五、iter方法

1、定义:

iter(...)
    iter(collection) -> iterator
    iter(callable, sentinel) -> iterator

    Get an iterator from an object.  In the first form, the argument must
    supply its own iterator, or be a sequence.
    In the second form, the callable is called until it returns the sentinel.

2、解释:得到一个对象的迭代器, 第一种调用方式,传入参数本身必须可迭代,或者是一个序列。

                第二张调用方式, callable会被一直调用直到返回与sentinel一样的内容。

​3、举例:

def _callable(a=[]):
    a.append(5)
    return a
b = [5,5,5]
c = iter(_callable, b)
for i in c:
    print i
输出:
[5]
[5, 5]

四十六、len方法

1、定义:

len(...)
    len(object) -> integer

    Return the number of items of a sequence or collection.

2、解释:返回一个集合或者序列的项目数量

3、举例:

>>> len(range(5))
5
>>> len('12345')
5
>>> len(set(['1','1','2','3']))
3

 


发表评论

* *