python循环技巧
来源:互联网 发布:windows多线程编程视频 编辑:程序博客网 时间:2024/05/22 10:23
python循环技巧
在字典中循环时,关键字和对应的值可以使用iteritems()方法同时解读出来。
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}>>> for k, v in knights.iteritems():... print k, v...gallahad the purerobin the brave
在序列中循环时,索引位置和对应值可以使用enumerate()函数同时得到。
>>> for i, v in enumerate(['tic', 'tac', 'toe']):... print i, v...0 tic1 tac2 toe
同时循环两个或更多的序列,可以使用 zip() 整体解读。
>>> questions = ['name', 'quest', 'favorite color']>>> answers = ['lancelot', 'the holy grail', 'blue']>>> for q, a in zip(questions, answers):... print 'What is your %s? It is %s.' % (q, a)...What is your name? It is lancelot.What is your quest? It is the holy grail.What is your favorite color? It is blue.
0 0
- python循环技巧
- python循环技巧
- Python学习笔记:循环技巧
- Python学习笔记-数据结构之循环技巧
- python中for循环的小技巧
- python中for循环/迭代的技巧
- Python笔记7:循环技巧和控制语句
- Python 小技巧:for 循环后面接 else 语句
- python循环
- Python循环
- python循环
- python 循环
- python -循环
- python 循环
- Python循环
- Python循环
- python循环
- Python 循环
- JNI学习
- 指针进阶之指针运算
- 揭开MVC,MVP,MVVM,VIPER这几种模式的神秘面纱
- Android 设计模式 之 单例模式
- Python文件I/O
- python循环技巧
- Redis 客户端使用Jedis建立连接池(一)
- 黑马程序员————JAVA基础之抽象类、接口
- 黑马程序员--Java编程之用户注册登录案例及MVC初识
- Java面试题及答案第一波袭来!
- 配置事务
- JMeter监控tomcat7性能
- [POJ3080] Blue Jeans
- The operation couldn’t be completed. (LaunchServicesError error 0.)问题解决