用于任意嵌套数据结构的python实用程序。
nifty-nesting的Python项目详细描述
漂亮的嵌套
用于操作任意嵌套数据结构的python实用程序。
包括:flatten
、map
、pack_into
、filter
、reduce
、assert_same_structure
深受internal nesting utilities in TensorFlow.
的启发作为嵌套结构的一部分,支持collections.Sequence
(list
,tuple
等)、collections.Mapping
(dict
等)、set
、namedtuple
和attr
数据类。
允许用户通过所有函数的is_atomic
参数指定哪些元素应被视为嵌套结构的一部分,哪些元素应被视为“原子”数据元素。
示例:
扁平化
返回包含嵌套结构的每个原子元素的列表。元素以确定的顺序返回。
importnifty_nestingasneststructure=[1,(2,{'a':3},4]flat=nest.flatten(structure)assertflat==[1,2,3,4]structure=([1,2],{'a':[3,4],'b':[5,6]})flat=nest.flatten(structure,is_atomic=lambdax:isinstance(x,list))assertflat==[[1,2],[3,4],[5,6]]
地图
映射嵌套结构的每个原子元素。
importnifty_nestingasneststructure={'a':[1,2],'b':(3,4,{'c':5})}mapped=nest.map(lambdax:2*x,structure)assertmapped=={'a':[2,4],'b':(6,8,{'c':10})}structure=([1,2],{'a':[3,4],'b':[5,6]})mapped=nest.map(lambdax:max(x),structure,is_atomic=lambdax:isinstance(x,list))assertmapped==(2,{'a':4,'b':6})
将列表打包到
将一个平面列表打包到任何具有相同原子元素数的任意结构中。元素以与flatten
创建的平面列表兼容的确定顺序打包。
importnifty_nestingasneststructure=(1,{'key':[2,{3,4},5]},[6,7])flat_list=['a','b','c','d','e','f','g']packed=nest.pack_list_into(structure,flat_list)assertpacked==('a',{'key':['b',{'c','d'},'e']},['f','g'])
文档
主要功能
扁平化(结构,是原子的=是标量的)
Returns a flattened list containing the atomic elements of `structure`.
The elements of `structure` are flattened in a deterministic order.
```
import nifty_nesting as nest
flat = nest.flatten([1, (2, {'a': 3}, 4])
assert flat == [1, 2, 3, 4]
```
Arguments:
structure: An arbitrarily nested structure of elements.
is_atomic: A function that returns `True` if a certain element
of `structure` ought to be treated as an atomic element, i.e.
not as part of the nesting structure.
Returns:
A list containing every atomic element of `structure`.
映射(func,structure,is_atomic=is_scalar)
Maps the atomic elements of `structure`.
```
import nifty_nesting as nest
structure = {'a': [1, 2], 'b': (3, 4, {'c': 5})}
mapped = nest.map(lambda x: 2*x, structure)
assert mapped == {'a': [2, 4], 'b': (6, 8, {'c': 10})}
```
Arguments:
func: The function to use to map atomic elements of `structure`.
structure: An arbitrarily nested structure of elements.
is_atomic: A function that returns `True` if a certain element
of `structure` ought to be treated as an atomic element, i.e.
not as part of the nesting structure.
Returns:
A structure with the same structure as `structure`, with the atomic elements
mapped according to `func`.
减少(func,structure,is_atomic=is_scalar):
Reduces the atomic elements of `structure`.
```
import nifty_nesting as nest
structure = {'a': [1, 2], 'b': (3, 4, {'c': 5})}
reduced = nest.reduce(lambda x, y: x+y, structure)
assert reduced == 15
```
Arguments:
func: The function to use to reduce atomic elements of `structure`.
structure: An arbitrarily nested structure of elements.
is_atomic: A function that returns `True` if a certain element
of `structure` ought to be treated as an atomic element, i.e.
not as part of the nesting structure.
Returns:
The reduced value.
过滤器(func,structure,keep_structure=true,is_atomic=is_scalar)
Filters the atomic elements of `structure`.
```
import nifty_nesting as nest
structure = {'a': [1, 2], 'b': (3, 4, {'c': 5})}
filtered = nest.filter(lambda x: x > 2, structure)
assert filtered == {'a': [], 'b': (3, 4, {'c': 5})}
filtered = nest.filter(lambda x: x > 2, structure, keep_structure=False)
assert filtered == {'b': (3, 4, {'c': 5})}
```
Arguments:
func: The function to use to filter atomic elements of `structure`.
structure: An arbitrarily nested structure of elements.
keep_structure: Whether or not to preserve empty substructures. If
`True`, these structures will be kept. If `False`, they will be
entirely filtered out.
is_atomic: A function that returns `True` if a certain element
of `structure` ought to be treated as an atomic element, i.e.
not as part of the nesting structure.
Returns:
The filtered elements of `structure` in the same structure as `structure`.
断言相同的结构(structure1,structure2,is_atomic=is_scalar)
Asserts that `structure1` and `structure2` have the same nested structure.
```
import nifty_nesting as nest
structure1 = {'a': [1, 2], 'b': (3, 4, {'c': 5})}
structure1 = {'a': ['a', 'b'], 'b': ('c', 'd', {'c': 'e'})}
nest.assert_same_structure(structure1, structure2)
```
Arguments:
structure1: An arbitrarily nested structure of elements.
structure2: An arbitrarily nested structure of elements.
is_atomic: A function that returns `True` if a certain element
of `structure` ought to be treated as an atomic element, i.e.
not as part of the nesting structure.
Raises:
`AssertionError` if the structures are not the same.
将列表打包成(结构、平面列表、原子=标量)
Packs the atomic elements of `flat_list` into the same structure as `structure`.
``
import nifty_nesting as nest
structure = {'a': [1, 2], 'b': (3, 4, {'c': 5})}
flat_list = [2, 4, 6, 7, 10]
packed = nest.pack_list_into(structure, flat_list)
assert packed == {'a': [2, 4], 'b': (6, 8, {'c': 10})}
```
Arguments:
structure: An arbitrarily nested structure of elements.
flat_list: A flat list with the same number of atomic elements as
`structure`.
is_atomic: A function that returns `True` if a certain element
of `structure` ought to be treated as an atomic element, i.e.
not as part of the nesting structure.
Returns:
A structure with the atomic elements of `flat_list` packed into the same
structure as `structure`.
is_atomic
的帮助函数
是标量(元素)
An `is_atomic` criterion. Returns `True` for scalar elements.
Scalar elements are : strings and any object that is not one of:
collections.Sequence, collections.Mapping, set, or attrs object.
```
import nifty_nesting as nest
flat = nest.flatten([1, [2, 3]], is_atomic=is_scalar)
assert flat == [1, 2, 3]
```
Arguments:
element: The element to check.
Returns:
`True` if the element is a scalar, else `False`.
具有最大深度(深度,是原子=是标量)
Returns an `is_atomic` criterion that checks the depth of a structure.
This function returns a function that can be passed to `is_atomic` to
preserve all structures up to a given depth.
```
import nifty_nesting as nest
flat = nest.flatten([[1, 2], [3, [4, 5]]], is_atomic=has_max_depth(1))
assert flat == [[1, 2], [3], [4, 5]]
```
Arguments:
depth: The maximum depth a structure can have in order to be considered
as an atomic element. For instance, `[1, 2, {'a': 3}]` has a depth of 2.
is_atomic: A function that returns `True` if a certain element
of `structure` ought to be treated as an atomic element, i.e.
not as part of the nesting structure.
Returns:
A function that can be passed to `is_atomic` to check for elements
with a depth of `depth` or less.
是序列(元素)
对于collections.Sequence
的实例,返回True
。
是映射(元素)
对于collections.Mapping
的实例,返回True
。
已设置(元素)
对于set
的实例,返回True
。
是双(元素)
对于namedtuple
的实例,返回True
。
是属性对象(元素)
为attr
修饰类的实例返回True
。