larray.Array.items

Array.items(axes=None, ascending=True) SequenceZip[source]

Return a (label, value) view of the array along axes.

Parameters
axesint, str or Axis or tuple of them, optional

Axis or axes along which to iterate and in which order. Defaults to None (all axes in the order they are in the array).

ascendingbool, optional

Whether to iterate the axes in ascending order (from start to end). Defaults to True.

Returns
Sequence

An object you can iterate (loop) on and index by position to get the Nth (label, value) couple along axes.

Examples

First, define a small helper function to make the following examples more readable.

>>> def str_key(key):
...     return tuple(str(k) for k in key)

Then create a test array:

>>> arr = ndtest((2, 2))
>>> arr
a\b  b0  b1
 a0   0   1
 a1   2   3

By default it iterates on all axes, in the order they are in the array.

>>> for key, value in arr.items():
...     print(str_key(key), "->", value)
('a0', 'b0') -> 0
('a0', 'b1') -> 1
('a1', 'b0') -> 2
('a1', 'b1') -> 3
>>> for key, value in arr.items(ascending=False):
...     print(str_key(key), "->", value)
('a1', 'b1') -> 3
('a1', 'b0') -> 2
('a0', 'b1') -> 1
('a0', 'b0') -> 0

but you can specify another axis order:

>>> for key, value in arr.items(('b', 'a')):
...     print(str_key(key), "->", value)
('b0', 'a0') -> 0
('b0', 'a1') -> 2
('b1', 'a0') -> 1
('b1', 'a1') -> 3

When you specify less axes than the array has, you get arrays back:

>>> # iterate on the "b" axis, that is return the (sub)array for each label along the "b" axis
... for key, value in arr.items('b'):
...     print(str_key(key), value, sep="\n")
('b0',)
a  a0  a1
    0   2
('b1',)
a  a0  a1
    1   3

One can also access elements of the items sequence directly, instead of iterating over it. Say we want to retrieve the first and last key-value pairs of our array, we could write:

>>> items = arr.items()
>>> first_key, first_value = items[0]
>>> str_key(first_key)
('a0', 'b0')
>>> first_value
0
>>> last_key, last_value = items[-1]
>>> str_key(last_key)
('a1', 'b1')
>>> last_value
3