关于itertools.groupby()

itertools.groupby()就是将相邻的并且相同的键值划分为同一组,相似功能可以看https://docs.python.org/3/library/itertools.html?highlight=groupby#itertools.groupby写的groupby类

>>> list_a
['A', 'A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'D', 'A', 'A', 'B', 'B', 'B']
>>> for date, items in groupby(list_a):
... print('date: {}'.format(date))
... for item in items:
... print(item, end=" ")
... print("\n==========")
...
date: A
A A A A
==========
date: B
B B B
==========
date: C
C C
==========
date: D
D
==========
date: A
A A
==========
date: B
B B B
==========

是不是发现上述例子还有可简化之处,毕竟A的分组要都归为一组(这是因为存在不相邻的A才出现的情况):

>>> list_a
['A', 'A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'D', 'A', 'A', 'B', 'B', 'B']
>>> list_a.sort(key=lambda list: list) # 经过lambda匿名函数排序后,将相邻的元素放在一起
>>> list_a
['A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'B', 'C', 'C', 'D']
>>> for date, items in groupby(list_a):
... print('date: {}'.format(date))
... for item in items:
... print(item, end=" ")
... print("\n==========")
...
date: A
A A A A A A
==========
date: B
B B B B B B
==========
date: C
C C
==========
date: D
D
==========

除了使用lambda匿名函数之外,还可以使用operator.itemgetter()函数,效率比lambda更快一些,具体可以看《Python Cookbook》

关于itertools.compress(data, selectors)

根据传递进去的选择器进行判断是否保留数据

>>> list1 = [1, 4, 7, 2, 98, 3, 6, 2]
>>> list_TF = [0,1,0,1,1,1,0,0]
>>> list_TF = [n ==1 for n in list_TF]
>>> list_TF
[False, True, False, True, True, True, False, False]
>>> from itertools import compress
>>> list(compress(list1, list_TF))
[4, 2, 98, 3]

其实通过教程我们还可以发现compress是大致如下:

>>> list1
[1, 4, 7, 2, 98, 3, 6, 2]
>>> list_TF
[False, True, False, True, True, True, False, False]
>>> [n for n,s in zip(list1, list_TF) if s]
[4, 2, 98, 3]

如果觉得慢,还可以使用生成器来代替

itertools.groupby()/itertools.compress() 笔记的更多相关文章

  1. [PY3]——根据某个特定的字段来分组迭代访问一个字段或序列?/ itertools.groupby( )函数

    问题 你有一个字典或者实例的序列,然后你想根据某个特定的字段(比如‘date’)来分组迭代访问. 解决方案 itertools.groupby( )函数 itertools.groupby(rows, ...

  2. itertools.groupby()分组字典列表

    ## itertools.groupby()分组字典列表数据 from operator import itemgetter from itertools import groupby student ...

  3. Python中itertools.groupby分组的使用

    Python中itertools.groupby分组的使用 有时候我们需要给一个列表按照某个属性分组,可以借助groupby来实现. 比如:一下列表我想以严重程度给它分组,并求出每组的元素个数. fr ...

  4. Python中的字典分组函数(groupby,itertools)

    from operator import itemgetter # itemgetter用来去dict中的key,省去了使用lambda函数 from itertools import groupby ...

  5. python编程零碎积累

    例行程序 def routine():     lastday = ''     while True:         day = datetime.datetime.now().strftime( ...

  6. Python列表中去重的多种方法

    怎么快速的对列表进行去重呢,去重之后原来的顺序会不会改变呢? 去重之后顺序会改变 set去重 列表去重改变原列表的顺序了 l1 = [1,4,4,2,3,4,5,6,1] l2 = list(set( ...

  7. Python学习笔记—itertools模块

    这篇是看wklken的<Python进阶-Itertools模块小结> 学习itertools模块的学习笔记 在看itertools中各函数的源代码时,刚开始还比较轻松,但后面看起来就比较 ...

  8. python笔记之itertools模块

    python笔记之itertools模块 itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生 ...

  9. Python标准库(1) — itertools模块

    简介 官方描述:Functional tools for creating and using iterators.即用于创建高效迭代器的函数. itertools.chain(*iterable) ...

随机推荐

  1. django面试大全

    1.Django请求的生命周期 a. wsgi, 创建socket服务端,用于接收用户请求并对请求进行初次封装. b. 中间件,对所有请求到来之前,响应之前定制一些操作. c. 路由匹配,在url和视 ...

  2. scrapy爬取动态分页内容

    1.任务定义: 爬取某动态分页页面中所有子话题的内容. 所谓"动态分页":是指通过javascript(简称"js")点击实现翻页,很多时候翻页后的页面地址ur ...

  3. 使用Redis构建支持程序

    在Linux和Unix世界中,有两种常见的记录日志的方法.第一种是将日志记录到文件里面,然后随着时间流逝不断地将一个有一个日志行添加到文件里面,并在一段时间之后创建新的日志文件.包括Redis在内的很 ...

  4. 【leetcode】344. Reverse String

    problem 344. Reverse String solution: class Solution { public: void reverseString(vector<char> ...

  5. [LeetCode&Python] Problem 387. First Unique Character in a String

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  6. [LeetCode&Python] Problem 733. Flood Fill

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  7. ZOJ - 4082:Little Sub and his Geometry Problem (双指针)

    Little Sub loves math very much, and has just come up with an interesting problem when he is working ...

  8. 各种浏览器兼容trim()的方法

    一.利用while方法解决 function trim(str) { while (str[0] == ' ') { str = str.slice(1); } while (str[str.leng ...

  9. FTP相关、用vsftpd搭建ftp、xshell使用xftp传输文件、使用pure-ftpd搭建ftp服务

    1.FTP相关(file transfer protocol,文件传输协议)   2.用vsftpd搭建ftp安装:yum install vsftpd -y创建一个虚拟用户:useradd  vft ...

  10. 2017.4.4 TCP/IP三次握手,四次挥手

    之前在电话面试的时候,被问到,所以找到一个超级容易理解的图片,自己保存,也算分享.