dask.bag.zip
dask.bag.zip¶
- dask.bag.zip(*bags)¶
按分区方式的袋装压缩
所有传递的袋子必须具有相同数量的分区。
注意:相应的分区应该具有相同的长度;如果不相同,较长分区中的“多余”元素将被丢弃。如果你遇到这种情况,很可能你需要的是像 pandas 那样的数据对齐机制,而不是像 zip_longest 这样的缺失值填充器。
示例
正确用法:
>>> import dask.bag as db >>> evens = db.from_sequence(range(0, 10, 2), partition_size=4) >>> odds = db.from_sequence(range(1, 10, 2), partition_size=4) >>> pairs = db.zip(evens, odds) >>> list(pairs) [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]
不正确的用法:
>>> numbers = db.range(31, npartitions=1) >>> fizz = numbers.filter(lambda n: n % 3 == 0) >>> buzz = numbers.filter(lambda n: n % 5 == 0) >>> fizzbuzz = db.zip(fizz, buzz) >>> list(fizzbuzz) [(0, 0), (3, 5), (6, 10), (9, 15), (12, 20), (15, 25), (18, 30)]
当你真正想要的是类似于以下内容时:
>>> list(fizzbuzz) (0, 0), (3, None), (None, 5), (6, None), (9, None), (None, 10), (12, None), (15, 15), (18, None), (None, 20), (21, None), (24, None), (None, 25), (27, None), (30, 30)