How can a reader traverse shards without first copying every sample?
chain.from_iterable consumes one inner iterable at a time, advancing to the next shard only after exhaustion.
Python example and interpretation
An empty shard between two populated shards contributes no rows. Flattening removes one level, so a string supplied as a shard would yield characters.
from itertools import chain
list(chain.from_iterable([[1,2],[],[3]]))Result [1, 2, 3]
中文
怎样逐段读取分片而不先复制全部样本?
chain.from_iterable 依次消费每个内部可迭代对象,前一分片耗尽后才进入下一分片。
Python 算例与适用条件
两个非空分片之间夹一个空分片,输出顺序保持不变。此操作只消除一层嵌套;作为分片的字符串会按字符展开。
from itertools import chain
list(chain.from_iterable([[1,2],[],[3]]))结果 [1, 2, 3]