當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python dask.bag.Bag.starmap用法及代碼示例

用法:

Bag.starmap(func, **kwargs)

使用給定包中的參數元組應用函數。

這類似於 itertools.starmap ,除了它還接受關鍵字參數。在偽代碼中,這可以寫成:

>>> def starmap(func, bag, **kwargs):
...     return (func(*args, **kwargs) for args in bag)

參數

func可調用的
**kwargs項目,延遲或對象,可選

要傳遞給 func 的額外關鍵字參數。這些可以是普通對象 dask.bag.Itemdask.delayed.Delayed

例子

>>> import dask.bag as db
>>> data = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
>>> b = db.from_sequence(data, npartitions=2)

對每個參數元組應用一個函數:

>>> from operator import add
>>> b.starmap(add).compute()
[3, 7, 11, 15, 19]

對每個參數元組應用一個函數,並帶有額外的關鍵字參數:

>>> def myadd(x, y, z=0):
...     return x + y + z
>>> b.starmap(myadd, z=10).compute()
[13, 17, 21, 25, 29]

關鍵字參數也可以是 dask.bag.Itemdask.delayed.Delayed 的實例:

>>> max_second = b.pluck(1).max()
>>> max_second.compute()
10
>>> b.starmap(myadd, z=max_second).compute()
[13, 17, 21, 25, 29]

相關用法


注:本文由純淨天空篩選整理自dask.org大神的英文原創作品 dask.bag.Bag.starmap。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。