首页 > 其他 > 详细

关于list.extend(iterable)

时间:2015-08-10 19:49:08      阅读:299      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!