栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

python列表中的不连续切片

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python列表中的不连续切片

itertools.compress
(2.7 / 3.1中的新功能)很好地支持了像这样的用例,尤其是当与结合使用时
itertools.cycle

from itertools import cycle, compressseq = range(100)criteria = cycle([True]*10 + [False]*20) # Use whatever pattern you like>>> list(compress(seq, criteria))[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

Python 2.7计时(相对于Sven的显式列表理解):

$ ./python -m timeit -s "a = range(100)" "[x for start in range(0, len(a), 30) for x in a[start:start+10]]"100000 loops, best of 3: 4.96 usec per loop$ ./python -m timeit -s "from itertools import cycle, compress" -s "a = range(100)" -s "criteria = cycle([True]*10 + [False]*20)" "list(compress(a, criteria))"100000 loops, best of 3: 4.76 usec per loop

Python 3.2计时(也相对于Sven的显式列表理解):

$ ./python -m timeit -s "a = range(100)" "[x for start in range(0, len(a), 30) for x in a[start:start+10]]"100000 loops, best of 3: 7.41 usec per loop$ ./python -m timeit -s "from itertools import cycle, compress" -s "a = range(100)" -s "criteria = cycle([True]*10 + [False]*20)" "list(compress(a, criteria))"100000 loops, best of 3: 4.78 usec per loop

可以看出,相对于2.7中的内联列表理解而言,它没有太大的区别,但是通过避免隐式嵌套范围的开销,在3.2中有很大帮助。

如果目标是遍历结果序列而不是将其转化为完全实现的列表,则在2.7中也可以看到类似的区别:

$ ./python -m timeit -s "a = range(100)" "for x in (x for start in range(0, len(a), 30) for x in a[start:start+10]): pass"100000 loops, best of 3: 6.82 usec per loop$ ./python -m timeit -s "from itertools import cycle, compress" -s "a = range(100)" -s "criteria = cycle([True]*10 + [False]*20)" "for x in compress(a, criteria): pass"100000 loops, best of 3: 3.61 usec per loop

对于特别长的模式,可以用类似

chain(repeat(True, 10), repeat(False,20))
这样的表达式替换模式表达式中的列表,这样就不必在内存中完全创建它。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/626307.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号