python3 zip()
2015-12-27 15:17
423 查看
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()用法并不简单。
>>> k = list(zip(*[[1,2,3],[4,5,6]]))
>>> k
[(1, 4), (2, 5), (3, 6)]
>>> k=zip(*[[1,2,3],[4,5,6]])
>>> k
<zip object at 0x000000000313F088>
>>> list(k)
[(1, 4), (2, 5), (3, 6)]
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()用法并不简单。
>>> k = list(zip(*[[1,2,3],[4,5,6]]))
>>> k
[(1, 4), (2, 5), (3, 6)]
>>> k=zip(*[[1,2,3],[4,5,6]])
>>> k
<zip object at 0x000000000313F088>
>>> list(k)
[(1, 4), (2, 5), (3, 6)]
相关文章推荐
- Python界面设计之Label
- pyhton 正则表达式学习
- Python进阶之路---1.1python简介
- 零基础入门学习Python(22):魔法方法(2)算术运算
- 《python科学计算》matplotlib学习
- python email.message:比较电子邮件
- 深入理解 Python import
- Python 线程(threading) 进程(multiprocessing)
- Python之email
- Python――pip, virtualenv, fabric三大神器
- python小白-day1
- 我的Python成长之路---第一天---Python基础(1)---2015年12月26日(雾霾)
- 排序算法—堆排序算法分析与实现(Python)
- Python项目管理
- 读书笔记--python数据可视化--005_导出数据到JSON、CSV和EXcel
- python 自定义信号处理器
- [Python]sqlite3二进制文件存储问题(BLOB)(You must not use 8-bit bytestrings unless you use a text_factory...)
- python模块学习之OS模块
- python基础篇
- 读书笔记--python数据可视化--004_读取JSON数据