python zip函数同时遍历两个数组和构造字典
2014-09-17 22:28
666 查看
>>> help(zip)
Help on class zip in module builtins:
class zip(object)
| zip(iter1 [,iter2 [...]]) --> zip object
|
| Return a zip object whose .__next__() method returns a tuple where
| the i-th element comes from the i-th iterable argument. The .__next__()
| method continues until the shortest iterable in the argument sequence
| is exhausted and then it raises StopIteration.
|
| Methods defined here:
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
|
| __iter__(...)
| x.__iter__() <==> iter(x)
|
| __next__(...)
| x.__next__() <==> next(x)
|
| __reduce__(...)
| Return state information for pickling.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __new__ = <built-in method __new__ of type object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
>>> A=[1,2,3]
>>> B=[4,5,6]
>>> bb=zip(A,B)
<zip object at 0x01EF08F0>
>>> for i in bb:
print(i)
(1, 4)
(2, 5)
(3, 6)
1.通过zip同时遍历两个数组
>>> for i,v in bb:
print(i,v)
1 4
2 5
3 6
通过这种方式同时遍历两个list,是不是很方便呢?但是如果两个list的长度不一样,则长的那一个的多余元素会被截掉。
2.通过zip构造字典
>>> key=['username','pwd']
>>> values=['nini','1qaz']
>>> bb=dict(zip(key,values))
>>> print(bb)
{'pwd': '1qaz', 'username': 'nini'}
Help on class zip in module builtins:
class zip(object)
| zip(iter1 [,iter2 [...]]) --> zip object
|
| Return a zip object whose .__next__() method returns a tuple where
| the i-th element comes from the i-th iterable argument. The .__next__()
| method continues until the shortest iterable in the argument sequence
| is exhausted and then it raises StopIteration.
|
| Methods defined here:
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
|
| __iter__(...)
| x.__iter__() <==> iter(x)
|
| __next__(...)
| x.__next__() <==> next(x)
|
| __reduce__(...)
| Return state information for pickling.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __new__ = <built-in method __new__ of type object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
>>> A=[1,2,3]
>>> B=[4,5,6]
>>> bb=zip(A,B)
<zip object at 0x01EF08F0>
>>> for i in bb:
print(i)
(1, 4)
(2, 5)
(3, 6)
1.通过zip同时遍历两个数组
>>> for i,v in bb:
print(i,v)
1 4
2 5
3 6
通过这种方式同时遍历两个list,是不是很方便呢?但是如果两个list的长度不一样,则长的那一个的多余元素会被截掉。
2.通过zip构造字典
>>> key=['username','pwd']
>>> values=['nini','1qaz']
>>> bb=dict(zip(key,values))
>>> print(bb)
{'pwd': '1qaz', 'username': 'nini'}
相关文章推荐
- Python用zip函数同时遍历多个迭代器示例详解
- 第11条:用zip函数同时遍历两个迭代器
- Python的zip函数——将两个列表转换成字典
- Python中不用循环同时遍历两个数组并进行对应元素的处理并进行删除
- Python用zip函数同时遍历多个迭代器示例详解
- 【Python】无须numpy,利用map函数与zip(*)函数对数组转置
- python同时遍历数组的索引和元素
- python之遍历数组、字典
- Python基础之——使用字典和函数构造switch
- python构造字典的dict()函数理解
- Python实遍历二维数组题目一:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
- Python 并行遍历zip()函数使用方法(更新日期:2017-07-11)
- python笔记之:两个数组同时排序
- python同时遍历两个list
- 【Python】无须numpy,利用map函数与zip(*)函数对数组转置(转)
- python同时遍历两个list
- [C++]for同时遍历两个数组
- python知识索引-数组、矩阵、字典、函数
- Python中数组 链表 元组 字典实现类Sql多字段排序和动态函数实现
- Python直接用字典数组导入到函数的非固定参数