Chinaunix首页 | 论坛 | 博客
  • 博客访问: 31406
  • 博文数量: 5
  • 博客积分: 180
  • 博客等级: 入伍新兵
  • 技术积分: 60
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-31 17:49
文章分类

全部博文(5)

文章存档

2011年(5)

我的朋友

分类: Python/Ruby

2011-10-08 17:57:35

先看文档:zip(*iterables)

Make an iterator that aggregates elements from each of the iterables.
Returns an iterator of tuples, where the i-th tuple contains the
i-th element from each of the argument sequences or iterables. The
iterator stops when the shortest input iterable is exhausted. With a single
iterable argument, it returns an iterator of 1-tuples. With no arguments, it
returns an empty iterator. Equivalent to:
def zip(*iterables):
    # zip('ABCD', 'xy') --> Ax By
    sentinel = object()
    iterables = [iter(it) for it in iterables]
    while iterables:
        result = []
        for it in iterables:
            elem = next(it, sentinel)
            if elem is sentinel:
                return
            result.append(elem)
        yield tuple(result)
举例:
>>> k = list(zip(*[[1,2,3],[4,5,6]]))
>>> k
[(1, 4), (2, 5), (3, 6)]

zip()是内置函数, 能把迭代对象进行聚合,返回值是迭代对象-聚合后的元组,用list()函数把它转化为列表

文档那个等价函数值得学习,那个iter()、next()用法并不简单。
阅读(2667) | 评论(0) | 转发(0) |
0

上一篇:python 3.2笔记 字符串格式化

下一篇:没有了

给主人留下些什么吧!~~