3-6如何在一个for语句中迭代多个可迭代对象

1、并行迭代
迭代元组可以进行拆包迭代。
>>> zip([1,2,3,4],('a','b','c','d'))
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
>>> zip([1,2,3,4],('a','b','c')) #当元素个数不一致时,以少的为准
[(1, 'a'), (2, 'b'), (3, 'c')]
>>> zip([1,2,3,4],('a','b','c','d'),"")
[(1, 'a', ''), (2, 'b', ''), (3, 'c', ''), (4, 'd', '')]
>>> zip([1,2,3,4],('a','b','c'),"")
[(1, 'a', ''), (2, 'b', ''), (3, 'c', '')]
实现并行迭代例子实现
(1)生成三科成绩的3个列表
>>> from random import randint
>>> math = [randint(60,100) for _ in xrange(40)]
>>> english = [randint(60,100) for _ in xrange(40)]
>>> chinese = [randint(60,100) for _ in xrange(40)]
(2)由zip()函数将三科成绩的3个列表,组成一个元组。迭代元组可以进行拆包迭代
>>> for m,e,c in zip(math,english,chinese):
print (m+e+c),
输出结果:285 234 207 204 272 268 234 280 241 232 202 241 236 262 231 206 238 246 236 224 254 248 258 234 246 227 216 240 216 214 224 250 222 228 223 248 252 201 265 192
2、串行迭代
>>> from itertools import chain
>>> help(chain)
Help on class chain in module itertools: class chain(__builtin__.object)
| chain(*iterables) --> chain object
|
| Return a chain object whose .next() method returns elements from the
| first iterable until it is exhausted, then elements from the next
| iterable, until all of the iterables are exhausted.
|
| Methods defined here:
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
|
| __iter__(...)
| x.__iter__() <==> iter(x)
|
| from_iterable(...)
| chain.from_iterable(iterable) --> chain object
|
| Alternate chain() constructor taking a single iterable argument
| that evaluates lazily.
|
| next(...)
| x.next() -> the next value, or raise StopIteration
|
| ----------------------------------------------------------------------
| 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
help(chain)
>>> for x in chain([1,2,3,4],"abc"):
print x,
输出结果:1 2 3 4 a b c
串行迭代例子实现
(1)生成四个班级的学习的成绩列表,每班的人数不同
>>> from random import randint
>>> e1= [randint(60,100) for _ in xrange(40)]
>>> e2= [randint(60,100) for _ in xrange(46)]
>>> e3= [randint(60,100) for _ in xrange(50)]
>>> e4= [randint(60,100) for _ in xrange(40)]
(2)串行对各个班级进行迭代,判断每个学生成绩,大于90的将人数加1
>>> count = 0
>>> for x in chain(e1,e2,e3,e4):
if x>90:
count +=1 >>> count
输出结果:49
3-6如何在一个for语句中迭代多个可迭代对象的更多相关文章
- python迭代-如何在一个for语句中迭代多个可迭代对象
如何在一个for语句中迭代多个可迭代对象 问题举例 (1)某班学生期末考试成绩,语文,数学,英语分别存储在3个列表中,同时迭代三个列表,计算每个学生的总分 (2)某年级有4个班,某次考试每班英语成绩分 ...
- 如何在一个for语句中迭代多个对象(2.7)
如何在一个for语句中迭代多个对象 总结: 并行迭代使用zip(l1, l2, l3) 每次迭代从3个列表里各取一个数据 串行迭代使用itertools.chain(l1, l2, l3) 相当于把3 ...
- 8、如何实现可迭代对象和迭代器对象 9、如何使用生成器函数实现可迭代对象 10、如何进行反向迭代以及如何实现反向迭代 11、如何对迭代器做切片操作 12、如何在一个for语句中迭代多个可迭代对象
8.如何实现可迭代对象和迭代器对象 PS:注意重载Iterator方法的时候,需要和原来的方法名一样,否则创建实例时会报错 from collections import Iterator,Itera ...
- mysql 更新 语句中 的 safe_mode
在mysql5中,可以设置safe mode,比如在一个更新语句中UPDATE table_name SET bDeleted=0;执行时会错误,报:You are using safe update ...
- SQL语句中的select高级用法
#转载请联系 为了更好的了解下面的知识点,我们先创建两张表并插入数据. # 学生表 +----+-----------+------+--------+--------+--------+------ ...
- mybatis的xml中sql语句中in的写法(迭代遍历)
这里使用 foreach标签 <foreach item="item" collection="listTag" index="index&q ...
- 问题13:如何在for语句中迭代多个可迭代的对象
from random import randint a1 = [randint(10, 50) for _ in range(5)] a2 = [randint(10, 50) for _ in r ...
- MySQL 如何在一个语句中更新一个数值后返回该值 -- 自增长种子竞态问题处理
什么是竞态问题? 假设有一个计数器,首先当前值自增长,然后获取到自增长之后的当前值.自增长后的值有可能被有些操作用来当做唯一性标识,因此并发的操作不能允许取得相同的值. 为什么不能使用使用UPDATE ...
- python_如何在一个for循环中迭代多个可迭代对象?
案例: 某班学生期末考试成绩,语文.数学.英语分别存储在3个列表中,同时迭代三个列表.,计算每个学生的总分(并行) 某年级有4个班,某次英语成绩分别记录在4个列表中,依次迭代每个列表,统计全年级高于9 ...
随机推荐
- node.js入门学习(二)MIME模块,request和response对象,demo之不同url请求不同html页面,页面包含图片、样式css等静态资源
一.构建http服务程序-根据不同请求做出不同响应 // 加载http模块 var http = require("http"); // 创建一个http服务对象 http.cre ...
- #418 Div2 Problem B An express train to reveries (构造 || 全排列序列特性)
题目链接:http://codeforces.com/contest/814/problem/B 题意 : 有一个给出两个含有 n 个数的序列 a 和 b, 这两个序列和(1~n)的其中一个全排列序列 ...
- 为什么MongoDB适合大数据的存储?
NoSQL数据库都被贴上不同用途的标签,如MongoDB和CouchDB都是面向文档的数据库,但这并不意味着它们可以象JSON(JavaScript Object Notation,JavaScrip ...
- gdal test
https://blog.csdn.net/hb_programmer/article/details/81807699 gdal/ogr是一个光栅和矢量地理空间数据格式的翻译库,由开源地理空间基金会 ...
- 译-使用Scroll Snapping实现CSS控制页面滚动
特别声明,本文翻译自@alligatorio的Control Page Scroll in CSS Using Scroll Snapping一文,受限于译者能力,译文或存在不足,欢迎大家指出.如需转 ...
- springboot上传文件大小限制的配置
springboot配置文件: application.properties #配置文件传输 spring.servlet.multipart.enabled =true spring.servlet ...
- Oracle开发:normal ,sysdba,sysoper区别
Oracle将用户分成两类:[system]和[sys] [system]用户只能用normal身份登陆em.(可以看成公司的普通成员) [sys]用户具有“SYSDBA”(可以看成公司的CEO)或者 ...
- 快速找到oracle的alert日志
https://jingyan.baidu.com/article/f3ad7d0fe5d31309c3345b9b.html
- sensu
https://blog.csdn.net/enweitech/article/details/53763324
- linux测试某进程占用oi、cpu、内存的使用情况
pidstat 概述 pidstat是sysstat工具的一个命令,用于监控全部或指定进程的cpu.内存.线程.设备IO等系统资源的占用情况.pidstat首次运行时显示自系统启动开始的各项统计信息, ...