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语句中迭代多个可迭代对象的更多相关文章

  1. python迭代-如何在一个for语句中迭代多个可迭代对象

    如何在一个for语句中迭代多个可迭代对象 问题举例 (1)某班学生期末考试成绩,语文,数学,英语分别存储在3个列表中,同时迭代三个列表,计算每个学生的总分 (2)某年级有4个班,某次考试每班英语成绩分 ...

  2. 如何在一个for语句中迭代多个对象(2.7)

    如何在一个for语句中迭代多个对象 总结: 并行迭代使用zip(l1, l2, l3) 每次迭代从3个列表里各取一个数据 串行迭代使用itertools.chain(l1, l2, l3) 相当于把3 ...

  3. 8、如何实现可迭代对象和迭代器对象 9、如何使用生成器函数实现可迭代对象 10、如何进行反向迭代以及如何实现反向迭代 11、如何对迭代器做切片操作 12、如何在一个for语句中迭代多个可迭代对象

    8.如何实现可迭代对象和迭代器对象 PS:注意重载Iterator方法的时候,需要和原来的方法名一样,否则创建实例时会报错 from collections import Iterator,Itera ...

  4. mysql 更新 语句中 的 safe_mode

    在mysql5中,可以设置safe mode,比如在一个更新语句中UPDATE table_name SET bDeleted=0;执行时会错误,报:You are using safe update ...

  5. SQL语句中的select高级用法

    #转载请联系 为了更好的了解下面的知识点,我们先创建两张表并插入数据. # 学生表 +----+-----------+------+--------+--------+--------+------ ...

  6. mybatis的xml中sql语句中in的写法(迭代遍历)

    这里使用 foreach标签 <foreach  item="item" collection="listTag" index="index&q ...

  7. 问题13:如何在for语句中迭代多个可迭代的对象

    from random import randint a1 = [randint(10, 50) for _ in range(5)] a2 = [randint(10, 50) for _ in r ...

  8. MySQL 如何在一个语句中更新一个数值后返回该值 -- 自增长种子竞态问题处理

    什么是竞态问题? 假设有一个计数器,首先当前值自增长,然后获取到自增长之后的当前值.自增长后的值有可能被有些操作用来当做唯一性标识,因此并发的操作不能允许取得相同的值. 为什么不能使用使用UPDATE ...

  9. python_如何在一个for循环中迭代多个可迭代对象?

    案例: 某班学生期末考试成绩,语文.数学.英语分别存储在3个列表中,同时迭代三个列表.,计算每个学生的总分(并行) 某年级有4个班,某次英语成绩分别记录在4个列表中,依次迭代每个列表,统计全年级高于9 ...

随机推荐

  1. MySQL概述 - 一条查询sql语句的执行过程

    Server层 连接器 建立连接.获取权限.维持和管理连接. 连接建立比较复杂,建议使用长连接 定期断开长连接 mysql_reset_connection指令 查询缓存 建议关闭,任何更新操作会此t ...

  2. 开发一个chrome插件:将百度搜索热点屏蔽掉!

    每次百度搜索,搜索结果的右边总是出现些乱七八糟的搜索热点(推的都是些什么玩意,高校替课和我有毛关系,几个悲伤的热点我用星号顶掉了). 强迫症想把它隐藏掉,我用的是chrome浏览器,受adblock( ...

  3. MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main re

    出现这个问题是因为工程是应用win32,必须要有main函数,修改方式为: configuration properties中General->configuration Type->将a ...

  4. 常见的NullPointerException总结

    NullPointerException在这里简称为NPE 通过一些实例总结下常见的NPE问题: 1. 自动拆箱抛NPE 实体类: public class User { private String ...

  5. MongoDB接口类函数

    [接口类定义] [java] view plaincopy /** * 项目名:SpiderCrawler * 文件名:MongoDBDao.java * 描述:TODO(用一句话描述该文件做什么) ...

  6. java实现豆瓣回帖机器人

    最近一直帮老板写爬虫,写累了就寻思着找点乐子,碰巧平时喜欢逛豆瓣,就打算写一个自动回帖机器人,废话不多说我们进入正题: 主要用到2个开源工具:Jsoup和httpclient Step 1:模拟登陆 ...

  7. 五、RF中UI自动化操作基础

    列表分类 1.打开浏览器 Open Browser   url   browser [ url | browser=firefox | alias=None | remote_url=False | ...

  8. 自定义配置节点configSections的使用

    //App.config <?xml version="1.0" encoding="utf-8" ?><configuration>  ...

  9. vue 请求完接口后执行方法

    getLunbo: function() { var that = this; that.lunbo = []; // api.showProgress({ // title: '加载中' // }) ...

  10. Jmeter JDBC请求---把数据库结果参数化传递到其他请求

    摘要: 最近一个场景进行压力测试:生成商品id进行上下架和购买,记录写脚本的一个过程 1.在商品上架前需要准备商品ID,商品ID生成需要从数据库读取商品类别,从而生成商品ID,下面是从数据库:读取商品 ...