pwtools.comb.nested_loops

pwtools.comb.nested_loops(lists, flatten=False)[source]

Nested loops, optional flattening.

Parameters:
  • lists (list of sequences) – The objects to permute. len(lists) == the depth (nesting levels) of the equivalent nested loops. Individual lists may contain a mix of different types/objects, e.g. [[‘a’, ‘b’], [Foo(), Bar(), Baz()], [1,2,3,4,5,6,7]].

  • flatten (bool) – Flatten each entry in returned list.

Returns:

list

Return type:

nested lists

Examples

>>> from pwtools import comb
>>> comb.nested_loops([[1,2],['a','b']])
[[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]

If values of different lists should be varied together, use zip(). Note that you get nested lists back. Use flatten=True to get flattened lists.

>>> comb.nested_loops([(1,2), zip(['a','b'],(np.sin,np.cos))])
[[1, ['a', <ufunc 'sin'>]],
 [1, ['b', <ufunc 'cos'>]],
 [2, ['a', <ufunc 'sin'>]],
 [2, ['b', <ufunc 'cos'>]]]
>>> comb.nested_loops([(1,2), zip(['a','b'],(np.sin,np.cos))], flatten=True)
[[1, 'a', <ufunc 'sin'>],
 [1, 'b', <ufunc 'cos'>],
 [2, 'a', <ufunc 'sin'>],
 [2, 'b', <ufunc 'cos'>]]