2014年03月10日 星期一 10:04
zip的概念比较难以理解,它接收N个可遍历的数据,返回一个组合后的数据的迭代器。zip可以用来分解重构dict等数据结构。
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.
示例代码如下:
ages = { 'laomeng':32, 'laozhang':35, 'laoli':36, 'xiaowang':24, 'xiaohuang':28 } max_age=max(zip(ages.values(),ages.keys())) print(max_age) min_age=min(zip(ages.values(),ages.keys())) print(min_age) print() sages=sorted(zip(ages.values(),ages.keys())) for (age,name) in sages: print(name,age)
参考资料:
Zeuux © 2024
京ICP备05028076号