Python: itertools.compress()
定义:
itertools.compress()
输入:
iterable对象
相应的Boolean选择器序列
输出:
iterable对象中对应选择器为True的元素
用途:
当需要用另外一个相关联的序列来过滤某个序列的时候,这个函数非常有用
eg:
两个列表如下,其元素相对应,现请根据count输出address,条件只输出count大于5的对应地址:
addresses = [
'5412 N CLARK',
'5148 N CLARK',
'5800 E 58TH',
'2122 N CLARK'
'5645 N RAVENSWOOD',
'1060 W ADDISON',
'4801 N BROADWAY',
'1039 W GRANVILLE',
]
counts = [ 0, 3, 10, 4, 1, 7, 6, 1]
现在想将那些对应count 值大于5 的地址全部输出,可以这样做:
>>> from itertools import compress
>>> more5 = [n > 5 for n in counts]
>>> more5
[False, False, True, False, False, True, True, False]
>>> list(compress(addresses, more5))
['5800 E 58TH', '4801 N BROADWAY', '1039 W GRANVILLE']
这里的关键点在于先创建一个booleam序列,指示出哪些元素符合条件,然后compress()根据这个序列去选择输出对应位置为True的元素。
compress()返回的是一个迭代器,如果想得到一个列表,需要用list转换
Python: itertools.compress()的更多相关文章
- Python itertools模块详解
这货很强大, 必须掌握 文档 链接 http://docs.python.org/2/library/itertools.html pymotw 链接 http://pymotw.com/2/iter ...
- itertools.groupby()/itertools.compress() 笔记
关于itertools.groupby() itertools.groupby()就是将相邻的并且相同的键值划分为同一组,相似功能可以看https://docs.python.org/3/librar ...
- python itertools模块练习
参考 <python标准库> 也可以参考Vamei博客 列表用着很舒服,但迭代器不需要将所有数据同时存储在内存中. 本章练习一下python 标准库中itertools模块 合并 和 分解 ...
- [PY3]——过滤数据——列表推导、filter()、itertools.compress()
问题 你有一个数据序列,想利用一些规则从中提取出需要的值或者是缩短序列 解决方案 最简单的过滤数据的方法,就是使用列表推导. 使用列表推导的一个潜在缺陷就是如果输入非常大的时候会产生一个非常大的结果集 ...
- [python] itertools库学习
最近做 cyber-dojo上的题,好几道都要用到排列组合.一开始我还老老实实自己写算法.后来一想,不对呀!python有那么多的库,为啥不用呢? 于是搜了下,发现这个:itertools 使用 he ...
- 转:Python itertools模块
itertools Python的内建模块itertools提供了非常有用的用于操作迭代对象的函数. 首先,我们看看itertools提供的几个"无限"迭代器: >>& ...
- python, itertools模块
通过itertools模块,可以用各种方式对数据进行循环操作 1, chain() from intertools import chain for i in chain([1,2,3], ('a', ...
- python itertools 模块
Python的内建模块itertools提供了非常有用的用于操作迭代对象的函数 首先,我们看看itertools提供的几个“无限”迭代器: >>> import itertools ...
- 《笔记》Python itertools的groupby分组数据处理
今天遇到这么一个需求,需要将这样的数据进行分组处理: [(, ), (, ), (, ), (, ), (, ), (, )] 处理之后我可能需要得到这样的结果: [(, (, , (, , (, ) ...
随机推荐
- INTRO: THE DAWN (亡灵序曲) 中独白
As the last ship sailed towards the distant horizon I sat there watching on a rock My mind slowly dr ...
- 使用Wireshark分析QQ聊天
- R的transform
函数transform 作用:为原数据框添加新的列,改变原变量列的值,通过赋值NULL删除列变量 用法: transform(‘data’,….) data就是要修改的data, '…..'代表你要 ...
- centos6.9环境下JDK安装
1.准备jdk安装文件: 这里我使用的是 jdk-7u79-linux-x64.tar.gz 2.在 /usr/local 目录下创建 sotfware目录,并上传JDK文件: 解压文件并修改文件夹为 ...
- PAT甲1005 Spell it right【字符串】
1005 Spell It Right (20 分) Given a non-negative integer N, your task is to compute the sum of all th ...
- HDU 2993 - MAX Average Problem - [斜率DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 Consider a simple sequence which only contains p ...
- easyui-datagrid个人实例
这个实例数据表格的功能,可以实现分页,增删改查功能 1.user.jsp <%@ page language="java" contentType="text/ht ...
- USB--- kvm in ubuntu:
USB SS=USB SuperSpeed=USB 3.0!!顺应此前的USB 1.1 FullSpeed和USB 2.0 HighSpeed https://jingyan.baidu.com/ar ...
- 币安Binance API Websocket
本文介绍币安Binance API Websocket General WSS information The base endpoint is: wss://stream.binance.com:9 ...
- mysql python pymysql模块 获取插入的最后一条数据的自增ID lastrowid()方法
调用游标下的lastrowid 可以获取插入之前的表里id字段存放到哪个自增id cursor.lastrowid mysql> select * from userinfo; +----+-- ...