码迷,mamicode.com
首页 > 其他好文 > 详细

关于list.extend(iterable)

时间:2015-08-10 19:51:39      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

extend内的参数只要是iterable就可以,那么也可以添加定制的iterable,开整。

class A(object):
        def __init__(self):
                self.a = 0
        def __iter__(self):
                return self
        def next(self):
                self.a = self.a + 1
                if self.a > 10:
                        self.a = 0
                        raise StopIteration();
                return self.a

  

>>> import collections
>>> isinstance(a, collections.Iterable)
True
>>> import h
>>> a = h.A()
>>> for x in a:
...     print x,
... 
1 2 3 4 5 6 7 8 9 10
>>> l = list(‘sdfsd‘)
>>> l
[‘s‘, ‘d‘, ‘f‘, ‘s‘, ‘d‘]
>>> l.extend(a)
>>> l
[‘s‘, ‘d‘, ‘f‘, ‘s‘, ‘d‘, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

  

  

关于list.extend(iterable)

标签:

原文地址:http://www.cnblogs.com/iNeoWong/p/4719021.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!