Toolz 包為迭代器、函數和字典提供了一組實用函數。這些函數擴展了標準庫 itertools 和 functools,並大量借鑒了當代函數式語言的標準庫。該軟件包包含以下模塊 -
字典工具
函數 -
- assoc(d, 鍵, 值[, 工廠]) -返回具有新鍵值對的新字典。它不會修改初始字典。
import toolz d = toolz.dicttoolz.assoc({'Geeks':0}, 'forGeeks', 1) print(d)
輸出 -
{'Geeks': 0, 'forGeeks': 1}
- assoc_in(d, 鍵, 值[, 工廠]) -返回一個新的字典,其中包含新的、可能嵌套的鍵值對
import toolz d = toolz.dicttoolz.assoc_in({'Geeks':0}, 'forGeeks', 1) print(d)
輸出 -
{‘Geeks’: 0, ‘f’: {‘o’: {‘r’: {‘G’: {‘e’: {‘e’: {‘k’: {‘s’: 1}}}}}}}}
- dissoc(d, *鍵) -返回刪除給定鍵的新字典。它不會修改初始字典。
import toolz d = toolz.dicttoolz.dissoc({'g':0, 'e':1, 'k':2, 's':3}, 'k', 'e') print(d)
輸出 -
{'g': 0, 's': 3}
- get_in(鍵,ds[,默認值,no_default]) -返回 ds[I0][I1]…[IX],其中 [I0, I1, …, IX] 是鍵,ds 是嵌套字典。如果找不到 ds[I0][I1]…[IX],則返回“default”。
import toolz nested_dict ={'d1':{'k1':'v1', 'k2':'v2'}, 'd2':{'k3':{'d2A':{'k4':'v4'}}}} d = toolz.dicttoolz.get_in(['d1'], nested_dict) print(d) d = toolz.dicttoolz.get_in(['d2', 'k3', 'd2A'], nested_dict) print(d)
輸出 -
{'k1': 'v1', 'k2': 'v2'} {'k4': 'v4'}
- itemfilter(謂詞, d[, 工廠]) -它按項目過濾字典中的項目。
import toolz def func(item): key, val = item return key == ord(val)-65 d = {0:'A', 1:'B', 3:'C', 5:'F'} print(toolz.dicttoolz.itemfilter(func, d))
輸出 -
{0: 'A', 1: 'B', 5: 'F'}
- itemmap(func, d[, 工廠]) -將函數應用於字典的項目。
import toolz d = {0:'A', 1:'B', 3:'C', 5:'F'} print(toolz.dicttoolz.itemmap(reversed, d))
輸出 -
{'A': 0, 'B': 1, 'C': 3, 'F': 5}
- keyfilter(謂詞, d[, 工廠]) -它按鍵過濾字典中的項目。
import toolz def func(key): return 5<= len(key)<7 d = {'python': 0, 'julia': 1, 'java': 3, 'javascript': 5} print(toolz.dicttoolz.keyfilter(func, d))
輸出 -
{'python': 0, 'julia': 1}
- 鍵映射(func,d[,工廠] -將函數應用於字典的鍵。
import toolz def func(key): return ''.join(reversed(key)) d = {'python': 0, 'julia': 1, 'java': 3, 'javascript': 5} print(toolz.dicttoolz.keymap(func, d))
輸出 -
{'nohtyp': 0, 'ailuj': 1, 'avaj': 3, 'tpircsavaj': 5}
- 合並(*dicts,**kwargs)-它合並了字典的集合。
import toolz dict1 = {1:1, 2:4} dict2 = {3:9, 2:8, 4:16} print(toolz.dicttoolz.merge(dict1, dict2))
輸出 -
{1: 1, 2: 8, 3: 9, 4: 16}
- merge_with(func, *dicts, **kwargs) -合並字典並將函數應用於組合值。
import toolz dict1 = {1:1, 2:4} dict2 = {3:9, 2:8, 1:1} print(toolz.dicttoolz.merge_with(sum, dict1, dict2))
輸出 -
{1: 2, 2: 12, 3: 9}
- update_in(d, 鍵, func[, 默認, 工廠]) -更新嵌套字典中的值。如果 key = [k0, .., kX] 且 d[k0, …, kX] = value,update_in 返回原始字典的副本,其中 ‘value’ 替換為 func(value)。
import toolz def func(value): return value//2 nested_dict = {1:{11:111}, 2:{22:222}} print(toolz.dicttoolz.update_in(nested_dict, [1, 11], func))
輸出 -
{1: {11: 55}, 2: {22: 222}}
- valfilter(謂詞, d[, 工廠]) -按值過濾字典中的項目。
import toolz def func(value): return 4<len(value)<7 d = {0: 'python', 1: 'julia', 3: 'java', 5: 'javascript'} print(toolz.dicttoolz.valfilter(func, d))
輸出 -
{0: 'python', 1: 'julia'}
- valmap(func, d[, 工廠]) -將函數應用於字典的值。
import toolz def func(value): return ''.join(reversed(value)) d = {0: 'python', 1: 'julia', 3: 'java', 5: 'javascript'} print(toolz.dicttoolz.valmap(func, d))
輸出 -
{0: 'nohtyp', 1: 'ailuj', 3: 'avaj', 5: 'tpircsavaj'}
函數工具
函數 -
- 應用(*func_and_args,**kwargs)-它隻是應用一個函數並返回結果。
import toolz def double(n): return n + n print(toolz.functoolz.apply(double, 2))
輸出 -
4
- 補碼(函數)-顧名思義,它轉換返回所提供輸入的邏輯補碼。
import toolz def is_mulitple_of_5(n): return n % 5 == 0 not_multiple_of_5 = toolz.functoolz.complement(is_mulitple_of_5) print(is_mulitple_of_5(10)) print(not_multiple_of_5(10))
輸出 -
True False
- 撰寫(*函數)-它返回一個按順序應用其他函數的函數。函數從右到左應用。如果未提供參數,則返回恒等函數 (f(x) = x)。
import toolz def func(n): return n + n def square(n): return n * n x = toolz.functoolz.compose(func, square)(3) print(x)
輸出 -
18
- compose_left(*函數) -它返回一個按順序應用其他函數的函數。函數從左到右應用。如果未提供參數,則返回恒等函數 (f(x) = x)。
import toolz def func(n): return n + n def square(n): return n * n x = toolz.functoolz.compose_left(func, square)(3) print(x)
輸出 -
36
- flip -以相反的順序使用參數調用該函數。
import toolz def mod(a, b): return a % b print('7 % 3 :', toolz.functoolz.flip(mod, 3, 7))
輸出 -
7 % 3 : 1
- 身份(x) -恒等函數,簡單地返回x。
import toolz print(toolz.functoolz.identity(6))
輸出 -
6
- 管道(數據,*函數)-通過一係列函數傳遞一個值。它相當於compose_left(*funcs)
import toolz print(toolz.functoolz.pipe(3, double, square))
輸出 -
36
- thread_first(val, *形式) -通過一係列函數/表單來傳遞值。
import toolz def mod(a, b): return a % b def double(n): return n + n print(toolz.functoolz.thread_first(3, (mod, 2), double))
輸出 -
2
- thread_last(val, *形式) -通過一係列函數/表單來傳遞值。
import toolz def mod(a, b): return a % b def double(n): return n + n print(toolz.functoolz.thread_last(3, (mod, 2), double))
輸出 -
4
迭代工具
函數 -
- 累積(binop,seq [,初始])-這與‘reduce’函數類似。它重複地將函數應用於累積結果的序列。
import toolz from operator import add print(list(toolz.itertoolz.accumulate(add, [1, 2, 3, 4])))
輸出 -
[1, 3, 6, 10]
- 連接(序列)-它連接兩個或多個可迭代對象。
import toolz print(list(toolz.itertoolz.concat([[1], ['a'], [2, 3, 4]])))
輸出 -
[1, 'a', 2, 3, 4]
- 缺點(項目,序列)-它在序列的開頭添加‘item’。它相當於插入(0,項目)。
import toolz print(list(toolz.itertoolz.cons(1, ['a', 'b'])))
輸出 -
[1, 'a', 'b']
- diff(*seqs, **kwargs) -它比較兩個迭代中每個索引處的元素並返回不同對的列表。
import toolz print(list(toolz.itertoolz.diff([1, 2, 3], [2, 2, 4])))
輸出 -
[(1, 2), (3, 4)]
- 刪除(n,seq)-它刪除序列的前 n 個元素並返回新序列。
import toolz print(list(toolz.itertoolz.drop(3, [2, 3, 2, 6, 4, 7])))
輸出 -
[6, 4, 7]
- 頻率(seq) -它返回一個字典,其中包含元素及其按順序計數。它相當於collections.Counter。
import toolz print(toolz.itertoolz.frequencies(['c', 'b', 'c', 'b', 'd', 'e', 'h', 'h', 'b']))
輸出 -
{'c': 2, 'b': 3, 'd': 1, 'e': 1, 'h': 2}
- groupby(func, seq) -它根據 func 對序列元素進行分組後返回一個字典。
import toolz print(toolz.itertoolz.groupby(len, ['geeks', 'for', 'geeks']))
輸出 -
{5: ['geeks', 'geeks'], 3: ['for']}
- isdistinct(seq) -如果序列中的所有元素都不同,則返回 True,否則返回 False。
import toolz print(toolz.itertoolz.isdistinct('geeks'))
輸出 -
False
- 可迭代 (x) -如果 x 是可迭代的,則返回 True,否則返回 False。
print(toolz.itertoolz.isiterable([10]))
Output - True
- 交錯(seq)-它交錯序列,即連接序列index-wise。
import toolz print(list(toolz.itertoolz.interleave([[10, 20], [5, 8, 11]])))
輸出 -
[10, 5, 20, 8, 11]
- topk(k, seq[, 鍵]) -它返回序列中前 k 個最大的元素。
import toolz print(list(toolz.itertoolz.topk(2, [10, 20, 5, 8, 11])))
輸出 -
[20, 11]
- 唯一(seq[,鍵])-它返回序列的不同元素,就像 set(seq) 一樣。
import toolz print(list(toolz.itertoolz.unique([10, 20, 5, 8, 10, 20])))
輸出 -
[10, 20, 5, 8]
- merge_sorted(*seqs, **kwargs) -它以這樣的方式合並已排序的可迭代對象,使得結果集合也已排序。
import toolz print(list(toolz.itertoolz.merge_sorted([5, 10, 20], [4, 12, 24])))
輸出 -
[4, 5, 10, 12, 20, 24]
- mapcat(func, seqs) -它將 func 應用於每個序列並連接結果。
import toolz print(list(toolz.itertoolz.mapcat(lambda iter: [e * 2 for e in iter], [[5, 10, 20], [4, 12, 24]])))
輸出 -
[10, 20, 40, 8, 24, 48]
- 刪除(謂詞,序列)-它返回序列中謂詞為 False 的那些元素。它是濾波器的補函數。
import toolz print(list(toolz.itertoolz.remove(lambda x: x % 2 == 0, [5, 21, 4, 12, 24])))
輸出 -
[5, 21]
食譜
函數 -
- countby(鍵, seq) -通過關鍵函數對集合中的元素進行計數。
import toolz def iseven(n): return n % 2 == 0 print(toolz.recipes.countby(iseven, [12, 123, 1234]))
輸出 -
{True: 2, False: 1}
- 分區(func,seq)-根據給定函數劃分序列。
import toolz def iseven(n): return n % 2 == 0 print(list(toolz.recipes.partitionby(iseven, [12, 123, 31, 1234])))
輸出 -
[(12, ), (123, 31), (1234, )]
沙箱
函數 -
- 並行.fold(binop, seq[, 默認, Map, …] -減少但不保證有序減少。
import toolz def sum(a, b): return a + b print(toolz.sandbox.parallel.fold(sum, [1, 2, 3, 4]))
輸出 -
10
- core.unzip(seq) -拉鏈的倒數。
import toolz l1, l2 = toolz.sandbox.core.unzip([(0, 1), (1, 2), (2, 3)]) print(list(l1), list(l2))
輸出 -
[0, 1, 2] [1, 2, 3]
- countby(鍵, seq) -通過關鍵函數對集合中的元素進行計數。
相關用法
- Python Touch用法及代碼示例
- Python Tuple count()用法及代碼示例
- Python Tuple index()用法及代碼示例
- Python Tuple cmp()用法及代碼示例
- Python Tuple len()用法及代碼示例
- Python Tuple max()用法及代碼示例
- Python Tuple min()用法及代碼示例
- Python Tuple tuple()用法及代碼示例
- Python Tkinter grid()用法及代碼示例
- Python TextCalendar formatmonth()用法及代碼示例
- Python TextCalendar formatyear()用法及代碼示例
- Python TextCalendar prmonth()用法及代碼示例
- Python TextCalendar pryear()用法及代碼示例
- Python Thread getName()用法及代碼示例
- Python Thread is_alive()用法及代碼示例
- Python Thread join()用法及代碼示例
- Python Thread run()用法及代碼示例
- Python Thread setName()用法及代碼示例
- Python Thread start()用法及代碼示例
- Python Timer cancel()用法及代碼示例
- Python Timer start()用法及代碼示例
- Python Tensorflow abs()用法及代碼示例
- Python Tensorflow acos()用法及代碼示例
- Python Tensorflow acosh()用法及代碼示例
- Python Tensorflow asin()用法及代碼示例
注:本文由純淨天空篩選整理自naina024大神的英文原創作品 Toolz module in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。