【3】数据分析-8-实用的函数和字典迭代器--toolz

PyToolz是一组实用功能的迭代器,函数和字典的集合。Toolz 支持 Python 2.6+ 和 Python 3.3+。

https://pypi.python.org/pypi/toolz/

官网说明:https://toolz.readthedocs.io/en/latest/

相关函数详解:https://toolz.readthedocs.io/en/latest/api.html#itertoolz

Toolz 的实现包括三部分:

  •  itertoolz,for operations on iterables. 例如:groupby,unique, interpose,
  • functoolz, for higher-order functions. 例如:memoize,curry, compose
  • dicttoolz, for operations on dictionaries. 例如:assoc,update-in, merge.

一、itertoolz

二、functoolz

三、dicttoolz

1.toolz.dicttoolz.get_in(keys, coll, default=None, no_default=False)

get_in is a generalization of operator.getitem for nested data structures such as dictionaries and lists.

>>> transaction = {'name': 'Alice',
...                'purchase': {'items': ['Apple', 'Orange'],
...                             'costs': [0.50, 1.25]},
...                'credit card': '5555-1234-1234-1234'}
>>> get_in(['purchase', 'items', 0], transaction)
'Apple'
>>> get_in(['name'], transaction)
'Alice'
>>> get_in(['purchase', 'total'], transaction)
>>> get_in(['purchase', 'items', 'apple'], transaction)
>>> get_in(['purchase', 'items', 10], transaction)
>>> get_in(['purchase', 'total'], transaction, 0)
0
>>> get_in(['y'], {}, no_default=True)
Traceback (most recent call last):
    ...
KeyError: 'y'

参考资料:

http://www.open-open.com/lib/view/open1421475061718.html

个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn

Sam avatar
About Sam
专注生物信息 专注转化医学