zip函数解析
2015-09-14 22:24
483 查看
#-*-coding:utf-8-*- ''' Created on 2015年9月14日 @author: Zroad ''' #1、认识内置函数zip:使用多个序列作为参数,进行并行遍历 L1 = [1,2,3,4] L2 = [5,6,7,8] print type(zip(L1,L2)) #<type 'list'> """ 输出结果: [(1, 5), (2, 6), (3, 7), (4, 8)] 1 5 -- 6 2 6 -- 8 3 7 -- 10 4 8 -- 12 """ print zip(L1,L2) for (x,y) in zip(L1,L2): print x,y,"--",x+y #2、zip函数可接受多个序列作为参数,且序列的长度可不一致, #返回的结果中元组的个数同最短的序列的长度一致 """ 输出结果: [(1, 'S', {'A': 1}), (2, 'P', 3), (3, 'A', 'HELLO')] """ T1,T2,T3 = (1,2,3),'SPAM',[{'A':1},3,'HELLO','E'] print zip(T1,T2,T3) #3、使用zip函数生成字典 keys = ['spam','eggs','toast'] values = [1,2,3] D = {} for (k,v) in zip(keys,values): D[k] = v print D """ 另一种解决方案,直接使用内置的dict函数 """ print dict(zip(keys,values))
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例