提问人:Ben Blank 提问时间:1/12/2009 最后编辑:Ben Blank 更新时间:10/21/2023 访问量:249513
如何以块形式迭代列表
How to iterate over a list in chunks
问:
我有一个 Python 脚本,它以整数列表作为输入,我需要一次处理四个整数。不幸的是,我无法控制输入,否则我会将其作为四元素元组列表传入。目前,我正在以这种方式迭代它:
for i in range(0, len(ints), 4):
# dummy op for example code
foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3]
不过,它看起来很像“C-think”,这让我怀疑有一种更像 python 的方式来处理这种情况。该列表在迭代后将被丢弃,因此无需保留。也许这样的事情会更好?
while ints:
foo += ints[0] * ints[1] + ints[2] * ints[3]
ints[0:4] = []
不过,仍然不太“感觉”正确。:-/
更新:随着 Python 1.12 的发布,我更改了公认的答案。对于尚未(或不能)跳转到 1.12 的任何人,我鼓励您查看之前接受的答案或下面任何其他出色的、向后兼容的答案。
答:
在你的第二种方法中,我会通过这样做进入下一个 4 人组:
ints = ints[4:]
但是,我没有做过任何性能测量,所以我不知道哪一个可能更有效。
话虽如此,我通常会选择第一种方法。它并不漂亮,但这通常是与外界接触的结果。
import itertools
def chunks(iterable,size):
it = iter(iterable)
chunk = tuple(itertools.islice(it,size))
while chunk:
yield chunk
chunk = tuple(itertools.islice(it,size))
# though this will throw ValueError if the length of ints
# isn't a multiple of four:
for x1,x2,x3,x4 in chunks(ints,4):
foo += x1 + x2 + x3 + x4
for chunk in chunks(ints,4):
foo += sum(chunk)
另一种方式:
import itertools
def chunks2(iterable,size,filler=None):
it = itertools.chain(iterable,itertools.repeat(filler,size-1))
chunk = tuple(itertools.islice(it,size))
while len(chunk) == size:
yield chunk
chunk = tuple(itertools.islice(it,size))
# x2, x3 and x4 could get the value 0 if the length is not
# a multiple of 4.
for x1,x2,x3,x4 in chunks2(ints,4,0):
foo += x1 + x2 + x3 + x4
评论
size
len
chunk_size = 4
for i in range(0, len(ints), chunk_size):
chunk = ints[i:i+chunk_size]
# process chunk of size <= chunk_size
评论
def chunker(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
适用于任何序列:
text = "I am a very, very helpful text"
for group in chunker(text, 7):
print(repr(group),)
# 'I am a ' 'very, v' 'ery hel' 'pful te' 'xt'
print('|'.join(chunker(text, 10)))
# I am a ver|y, very he|lpful text
animals = ['cat', 'dog', 'rabbit', 'duck', 'bird', 'cow', 'gnu', 'fish']
for group in chunker(animals, 3):
print(group)
# ['cat', 'dog', 'rabbit']
# ['duck', 'bird', 'cow']
# ['gnu', 'fish']
评论
itertools
chunker
generator
return [...]
yield
for pos in xrange(0, len(seq), size): yield seq[pos:pos + size]
__getitem__
chunker()
如果列表很大,则执行此操作的最佳方法是使用生成器:
def get_chunk(iterable, chunk_size):
result = []
for item in iterable:
result.append(item)
if len(result) == chunk_size:
yield tuple(result)
result = []
if len(result) > 0:
yield tuple(result)
for x in get_chunk([1,2,3,4,5,6,7,8,9,10], 3):
print x
(1, 2, 3)
(4, 5, 6)
(7, 8, 9)
(10,)
评论
iterable = range(100000000)
chunksize
from itertools import izip_longest
def chunker(iterable, chunksize, filler):
return izip_longest(*[iter(iterable)]*chunksize, fillvalue=filler)
评论
izip_longest
zip_longest
修改自 Python 的 itertools 文档的
Recipes 部分:
from itertools import zip_longest
def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
例
grouper('ABCDEFGHIJ', 3, 'x') # --> 'ABC' 'DEF' 'GHI' 'Jxx'
注意:在 Python 2 上,使用 代替 .izip_longest
zip_longest
评论
izip_longest
None
由于还没有人提到它,这里有一个解决方案:zip()
>>> def chunker(iterable, chunksize):
... return zip(*[iter(iterable)]*chunksize)
仅当您的序列长度始终可被块大小整除时,它才有效,或者您不关心尾随块(如果不是)。
例:
>>> s = '1234567890'
>>> chunker(s, 3)
[('1', '2', '3'), ('4', '5', '6'), ('7', '8', '9')]
>>> chunker(s, 4)
[('1', '2', '3', '4'), ('5', '6', '7', '8')]
>>> chunker(s, 5)
[('1', '2', '3', '4', '5'), ('6', '7', '8', '9', '0')]
或者使用 itertools.izip 返回迭代器而不是列表:
>>> from itertools import izip
>>> def chunker(iterable, chunksize):
... return izip(*[iter(iterable)]*chunksize)
可以使用@ΤΖΩΤΖΙΟΥ的答案来修复填充:
>>> from itertools import chain, izip, repeat
>>> def chunker(iterable, chunksize, fillvalue=None):
... it = chain(iterable, repeat(fillvalue, chunksize-1))
... args = [it] * chunksize
... return izip(*args)
评论
使用 map() 而不是 zip() 修复了 J.F. Sebastian 回答中的填充问题:
>>> def chunker(iterable, chunksize):
... return map(None,*[iter(iterable)]*chunksize)
例:
>>> s = '1234567890'
>>> chunker(s, 3)
[('1', '2', '3'), ('4', '5', '6'), ('7', '8', '9'), ('0', None, None)]
>>> chunker(s, 4)
[('1', '2', '3', '4'), ('5', '6', '7', '8'), ('9', '0', None, None)]
>>> chunker(s, 5)
[('1', '2', '3', '4', '5'), ('6', '7', '8', '9', '0')]
评论
itertools.izip_longest
itertools.zip_longest
map
None
这个问题的理想解决方案适用于迭代器(而不仅仅是序列)。它也应该很快。
这是 itertools 文档提供的解决方案:
def grouper(n, iterable, fillvalue=None):
#"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return itertools.izip_longest(fillvalue=fillvalue, *args)
在我的 mac book air 上使用 ipython,我每个循环得到 47.5 个我们。%timeit
但是,这真的对我不起作用,因为结果被填充为偶数大小的组。没有填充的解决方案稍微复杂一些。最幼稚的解决方案可能是:
def grouper(size, iterable):
i = iter(iterable)
while True:
out = []
try:
for _ in range(size):
out.append(i.next())
except StopIteration:
yield out
break
yield out
简单,但速度很慢:每个循环 693 us
我能想到的用于内部循环的最佳解决方案:islice
def grouper(size, iterable):
it = iter(iterable)
while True:
group = tuple(itertools.islice(it, None, size))
if not group:
break
yield group
使用相同的数据集,我每个循环得到 305 个 us。
由于无法更快地获得纯解决方案,我提供了以下解决方案,并有一个重要的警告:如果您的输入数据中有 的实例,您可能会得到错误的答案。filldata
def grouper(n, iterable, fillvalue=None):
#"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
# itertools.zip_longest on Python 3
for x in itertools.izip_longest(*args, fillvalue=fillvalue):
if x[-1] is fillvalue:
yield tuple(v for v in x if v is not fillvalue)
else:
yield x
我真的不喜欢这个答案,但它要快得多。每个回路 124 us
评论
itertools
map
map
imap
def grouper(n, it): return takewhile(bool, map(tuple, starmap(islice, repeat((iter(it), n)))))
fillvalue
fillvalue = object()
if
if i[-1] is fillvalue:
yield tuple(v for v in i if v is not fillvalue)
iterable
islice
n
izip_longest
yield i[:modulo]
args
args = (iter(iterable),) * n
None
if None in i
yield
另一个答案,其优点是:
1) 易于理解
2) 适用于任何可迭代对象,而不仅仅是序列(上述一些答案会因文件句柄而窒息) 3) 不会一次
将块全部加载到内存中 4) 不会在内存
中对同一迭代器进行引用的块长列表 5)
列表末尾没有填充值
话虽如此,我没有计时,所以它可能比一些更聪明的方法慢,而且考虑到用例,一些优点可能无关紧要。
def chunkiter(iterable, size):
def inneriter(first, iterator, size):
yield first
for _ in xrange(size - 1):
yield iterator.next()
it = iter(iterable)
while True:
yield inneriter(it.next(), it, size)
In [2]: i = chunkiter('abcdefgh', 3)
In [3]: for ii in i:
for c in ii:
print c,
print ''
...:
a b c
d e f
g h
更新:由于内部循环和外部循环从同一迭代器中提取值,因此存在一些缺点:
1)继续在外部循环中无法按预期工作 - 它只是继续到下一项,而不是跳过一个块。但是,这似乎不是问题,因为在外部循环中没有什么可测试的。
2) break 在内部循环中没有按预期工作 - 控制将再次在内部循环中结束,迭代器中的下一个项目。要跳过整个块,要么将内部迭代器(上面的 ii)包装在一个元组中,例如 ,要么设置一个标志并耗尽迭代器。for c in tuple(ii)
与其他提案类似,但不完全相同,我喜欢这样做,因为它简单易读:
it = iter([1, 2, 3, 4, 5, 6, 7, 8, 9])
for chunk in zip(it, it, it, it):
print chunk
>>> (1, 2, 3, 4)
>>> (5, 6, 7, 8)
这样你就不会得到最后一个部分块。如果要获取最后一个块,只需使用 from .(9, None, None, None)
izip_longest
itertools
评论
zip(*([it]*4))
使用小功能和东西真的没有吸引力;我更喜欢只使用切片:
data = [...]
chunk_size = 10000 # or whatever
chunks = [data[i:i+chunk_size] for i in xrange(0,len(data),chunk_size)]
for chunk in chunks:
...
评论
len
itertools.repeat
itertools.cycle
我需要一个也适用于发电机组和发电机的解决方案。我想不出任何非常简短和漂亮的东西,但至少它是相当可读的。
def chunker(seq, size):
res = []
for el in seq:
res.append(el)
if len(res) == size:
yield res
res = []
if res:
yield res
列表:
>>> list(chunker([i for i in range(10)], 3))
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
设置:
>>> list(chunker(set([i for i in range(10)]), 3))
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
发电机:
>>> list(chunker((i for i in range(10)), 3))
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
def group_by(iterable, size):
"""Group an iterable into lists that don't exceed the size given.
>>> group_by([1,2,3,4,5], 2)
[[1, 2], [3, 4], [5]]
"""
sublist = []
for index, item in enumerate(iterable):
if index > 0 and index % size == 0:
yield sublist
sublist = []
sublist.append(item)
if sublist:
yield sublist
评论
另一种方法是使用以下两个参数形式:iter
from itertools import islice
def group(it, size):
it = iter(it)
return iter(lambda: tuple(islice(it, size)), ())
这可以很容易地适应使用填充(这类似于 Markus Jarderot 的答案):
from itertools import islice, chain, repeat
def group_pad(it, size, pad=None):
it = chain(iter(it), repeat(pad))
return iter(lambda: tuple(islice(it, size)), (pad,) * size)
这些甚至可以组合成可选的填充:
_no_pad = object()
def group(it, size, pad=_no_pad):
if pad == _no_pad:
it = iter(it)
sentinel = ()
else:
it = chain(iter(it), repeat(pad))
sentinel = (pad,) * size
return iter(lambda: tuple(islice(it, size)), sentinel)
评论
from funcy import partition
for a, b, c, d in partition(4, ints):
foo += a * b * c * d
这些函数还具有迭代器版本和 ,在这种情况下效率会更高。ipartition
ichunks
您还可以查看它们的实现情况。
单行临时解决方案,用于以大小块的形式迭代列表x
4
-
for a, b, c, d in zip(x[0::4], x[1::4], x[2::4], x[3::4]):
... do something with a, b, c and d ...
要避免所有转换为列表,并且:import itertools
>>> for k, g in itertools.groupby(xrange(35), lambda x: x/10):
... list(g)
生产:
...
0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1 [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
2 [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
3 [30, 31, 32, 33, 34]
>>>
我检查了一下,它没有转换为列表或使用,所以我(认为)这将延迟每个值的解析,直到它实际使用。可悲的是,(目前)似乎没有一个可用的答案提供这种变化。groupby
len
显然,如果您需要依次处理每个项目,请在 g 上嵌套 a for 循环:
for k,g in itertools.groupby(xrange(35), lambda x: x/10):
for i in g:
# do what you need to do with individual items
# now do what you need to do with the whole group
我对此特别感兴趣的是需要使用生成器来批量向 gmail API 提交最多 1000 个更改:
messages = a_generator_which_would_not_be_smart_as_a_list
for idx, batch in groupby(messages, lambda x: x/1000):
batch_request = BatchHttpRequest()
for message in batch:
batch_request.add(self.service.users().messages().modify(userId='me', id=message['id'], body=msg_labels))
http = httplib2.Http()
self.credentials.authorize(http)
batch_request.execute(http=http)
评论
groupby(messages, lambda x: x/3)
groupby(enumerate(messages), lambda x: x[0]/3)
使用 NumPy,这很简单:
ints = array([1, 2, 3, 4, 5, 6, 7, 8])
for int1, int2 in ints.reshape(-1, 2):
print(int1, int2)
输出:
1 2
3 4
5 6
7 8
关于这里给出的解决方案:J.F. Sebastian
def chunker(iterable, chunksize):
return zip(*[iter(iterable)]*chunksize)
它很聪明,但有一个缺点 - 总是返回元组。如何获取字符串?
当然你可以写,但临时元组还是被构造了。''.join(chunker(...))
你可以通过编写 own 来摆脱临时元组,如下所示:zip
class IteratorExhausted(Exception):
pass
def translate_StopIteration(iterable, to=IteratorExhausted):
for i in iterable:
yield i
raise to # StopIteration would get ignored because this is generator,
# but custom exception can leave the generator.
def custom_zip(*iterables, reductor=tuple):
iterators = tuple(map(translate_StopIteration, iterables))
while True:
try:
yield reductor(next(i) for i in iterators)
except IteratorExhausted: # when any of iterators get exhausted.
break
然后
def chunker(data, size, reductor=tuple):
return custom_zip(*[iter(data)]*size, reductor=reductor)
用法示例:
>>> for i in chunker('12345', 2):
... print(repr(i))
...
('1', '2')
('3', '4')
>>> for i in chunker('12345', 2, ''.join):
... print(repr(i))
...
'12'
'34'
评论
zip
def chunker(iterable, n):
"""Yield iterable in chunk sizes.
>>> chunks = chunker('ABCDEF', n=4)
>>> chunks.next()
['A', 'B', 'C', 'D']
>>> chunks.next()
['E', 'F']
"""
it = iter(iterable)
while True:
chunk = []
for i in range(n):
try:
chunk.append(next(it))
except StopIteration:
yield chunk
raise StopIteration
yield chunk
if __name__ == '__main__':
import doctest
doctest.testmod()
我喜欢这种方法。它感觉简单而不神奇,支持所有可迭代类型,不需要导入。
def chunk_iter(iterable, chunk_size):
it = iter(iterable)
while True:
chunk = tuple(next(it) for _ in range(chunk_size))
if not chunk:
break
yield chunk
这里很蟒蛇(您也可以内联函数的主体)split_groups
import itertools
def split_groups(iter_in, group_size):
return ((x for _, x in item) for _, item in itertools.groupby(enumerate(iter_in), key=lambda x: x[0] // group_size))
for x, y, z, w in split_groups(range(16), 4):
foo += x * y + z * w
我从来不想要填充我的块,所以这个要求是必不可少的。我发现在任何可迭代对象上工作的能力也是必需的。鉴于此,我决定扩展公认的答案,https://stackoverflow.com/a/434411/1074659。
如果由于需要比较和筛选填充值而不需要填充,则此方法的性能会受到轻微影响。但是,对于较大的块大小,此实用程序的性能非常高。
#!/usr/bin/env python3
from itertools import zip_longest
_UNDEFINED = object()
def chunker(iterable, chunksize, fillvalue=_UNDEFINED):
"""
Collect data into chunks and optionally pad it.
Performance worsens as `chunksize` approaches 1.
Inspired by:
https://docs.python.org/3/library/itertools.html#itertools-recipes
"""
args = [iter(iterable)] * chunksize
chunks = zip_longest(*args, fillvalue=fillvalue)
yield from (
filter(lambda val: val is not _UNDEFINED, chunk)
if chunk[-1] is _UNDEFINED
else chunk
for chunk in chunks
) if fillvalue is _UNDEFINED else chunks
如果您不介意使用外部软件包,则可以使用iteration_utilties
1 中的 iteration_utilities.grouper
。它支持所有可迭代对象(而不仅仅是序列):
from iteration_utilities import grouper
seq = list(range(20))
for group in grouper(seq, 4):
print(group)
打印:
(0, 1, 2, 3)
(4, 5, 6, 7)
(8, 9, 10, 11)
(12, 13, 14, 15)
(16, 17, 18, 19)
如果长度不是组大小的倍数,它还支持填充(不完整的最后一组)或截断(丢弃不完整的最后一组)最后一个组:
from iteration_utilities import grouper
seq = list(range(17))
for group in grouper(seq, 4):
print(group)
# (0, 1, 2, 3)
# (4, 5, 6, 7)
# (8, 9, 10, 11)
# (12, 13, 14, 15)
# (16,)
for group in grouper(seq, 4, fillvalue=None):
print(group)
# (0, 1, 2, 3)
# (4, 5, 6, 7)
# (8, 9, 10, 11)
# (12, 13, 14, 15)
# (16, None, None, None)
for group in grouper(seq, 4, truncate=True):
print(group)
# (0, 1, 2, 3)
# (4, 5, 6, 7)
# (8, 9, 10, 11)
# (12, 13, 14, 15)
基准
我还决定比较一些上述方法的运行时间。它是一个对数-对数图,根据不同大小的列表分组为“10”个元素组。对于定性结果:越低意味着越快:
至少在这个基准测试中,表现最好。紧随其后的是克拉兹的接近。iteration_utilities.grouper
该基准测试是使用 simple_benchmark
1 创建的。用于运行此基准测试的代码为:
import iteration_utilities
import itertools
from itertools import zip_longest
def consume_all(it):
return iteration_utilities.consume(it, None)
import simple_benchmark
b = simple_benchmark.BenchmarkBuilder()
@b.add_function()
def grouper(l, n):
return consume_all(iteration_utilities.grouper(l, n))
def Craz_inner(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
@b.add_function()
def Craz(iterable, n, fillvalue=None):
return consume_all(Craz_inner(iterable, n, fillvalue))
def nosklo_inner(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
@b.add_function()
def nosklo(seq, size):
return consume_all(nosklo_inner(seq, size))
def SLott_inner(ints, chunk_size):
for i in range(0, len(ints), chunk_size):
yield ints[i:i+chunk_size]
@b.add_function()
def SLott(ints, chunk_size):
return consume_all(SLott_inner(ints, chunk_size))
def MarkusJarderot1_inner(iterable,size):
it = iter(iterable)
chunk = tuple(itertools.islice(it,size))
while chunk:
yield chunk
chunk = tuple(itertools.islice(it,size))
@b.add_function()
def MarkusJarderot1(iterable,size):
return consume_all(MarkusJarderot1_inner(iterable,size))
def MarkusJarderot2_inner(iterable,size,filler=None):
it = itertools.chain(iterable,itertools.repeat(filler,size-1))
chunk = tuple(itertools.islice(it,size))
while len(chunk) == size:
yield chunk
chunk = tuple(itertools.islice(it,size))
@b.add_function()
def MarkusJarderot2(iterable,size):
return consume_all(MarkusJarderot2_inner(iterable,size))
@b.add_arguments()
def argument_provider():
for exp in range(2, 20):
size = 2**exp
yield size, simple_benchmark.MultiArgument([[0] * size, 10])
r = b.run()
1 免责声明:我是库的作者,.iteration_utilities
simple_benchmark
评论
除非我遗漏了什么,否则没有提到以下带有生成器表达式的简单解决方案。它假设块的大小和数量都是已知的(通常是这样),并且不需要填充:
def chunks(it, n, m):
"""Make an iterator over m first chunks of size n.
"""
it = iter(it)
# Chunks are presented as tuples.
return (tuple(next(it) for _ in range(n)) for _ in range(m))
从 Python 3.8 开始,您可以使用 walrus 运算符和 .:=
itertools.islice
from itertools import islice
list_ = [i for i in range(10, 100)]
def chunker(it, size):
iterator = iter(it)
while chunk := list(islice(iterator, size)):
print(chunk)
In [2]: chunker(list_, 10)
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69]
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79]
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89]
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
more-itertools 包具有分块方法,它正是这样做的:
import more_itertools
for s in more_itertools.chunked(range(9), 4):
print(s)
指纹
[0, 1, 2, 3]
[4, 5, 6, 7]
[8]
chunked
返回列表中的项。如果您更喜欢可迭代对象,请使用 ichunked。
从 Python 3.12 开始,itertools
模块获得了一个批处理
函数,该函数专门用于迭代输入可迭代对象的批次,其中最终批次可能不完整(每个批次都是 )。根据文档中给出的示例代码:tuple
>>> for batch in batched('ABCDEFG', 3):
... print(batch)
...
('A', 'B', 'C')
('D', 'E', 'F')
('G',)
性能说明:
与迄今为止的所有函数一样,批处理
的实现位于 C 层,因此它能够进行 Python 级别代码无法匹配的优化,例如itertools
- 在每次拉取新批次时,它都会主动分配精确正确大小的元素(对于除最后一批之外的所有批次),而不是通过摊销增长逐个元素构建 up,从而导致多次重新分配(就像调用 an 的解决方案所做的那样)
tuple
tuple
tuple
islice
- 它只需要每批次查找一次底层迭代器的功能,而不需要每批次查找次数(基于方法的方式)
.__next__
n
zip_longest((iter(iterable),) * n)
- 对最终情况的检查是一个简单的 C 级检查(微不足道,无论如何都需要处理可能的异常)
NULL
- 处理最终情况是 C 后跟一个直接(不将副本复制成更小的)到已知的最终大小,因为它正在跟踪它成功拉取了多少元素(没有复杂的“创建哨兵以用作并执行 Python 级别/检查每个批次以查看它是否为空,最后一批需要搜索最后出现的位置, 创建基于解决方案所需的削减)。
goto
realloc
tuple
fillvalue
if
else
fillvalue
tuple
zip_longest
在所有这些优点之间,它应该大大优于任何 Python 级别的解决方案(甚至是高度优化的解决方案,将大部分或全部每个项目的工作推送到 C 层),无论输入可迭代对象是长还是短,也无论批大小和最终(可能不完整)批次的大小(基于 zip_longest
的解决方案是否使用保证的唯一填充值
s 以确保安全对于几乎所有不可用的情况,都是最好的解决方案,但在“大批量很少,最终批次大部分,没有完全填充”的病理情况下,它们可能会受到影响,尤其是在 3.10 之前,当不能用于优化从线性搜索到二进制搜索的切片时,但完全避免了该搜索,因此它根本不会遇到病理病例)。itertools.batched
bisect
fillvalue
O(n)
O(log n)
batched
评论
下一个:循环访问对象属性
评论