pymel.util.arrays.ArrayIter¶
- class ArrayIter(a[, axis1[, axis2[, ...]]])¶
A general purpose iterator on Arrays.
ArrayIter allows to iterate on one or more specified axis of an Array, in any order.
For an Array of n dimensions, iterator on p axis will yield sub-arrays of n-p dimensions, numerical components if n-p is 0.
>>> A = Array(range(1, 28), shape=(3, 3, 3)) >>> print A.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 23, 24], [25, 26, 27]]] >>> [a for a in A] [Array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), Array([[10, 11, 12], [13, 14, 15], [16, 17, 18]]), Array([[19, 20, 21], [22, 23, 24], [25, 26, 27]])] >>> [a for a in ArrayIter(A, 0)] [Array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), Array([[10, 11, 12], [13, 14, 15], [16, 17, 18]]), Array([[19, 20, 21], [22, 23, 24], [25, 26, 27]])] >>> [a for a in ArrayIter(A, 1)] [Array([[1, 2, 3], [10, 11, 12], [19, 20, 21]]), Array([[4, 5, 6], [13, 14, 15], [22, 23, 24]]), Array([[7, 8, 9], [16, 17, 18], [25, 26, 27]])] >>> [a for a in ArrayIter(A, 2)] [Array([[1, 4, 7], [10, 13, 16], [19, 22, 25]]), Array([[2, 5, 8], [11, 14, 17], [20, 23, 26]]), Array([[3, 6, 9], [12, 15, 18], [21, 24, 27]])] >>> [a for a in ArrayIter(A, 0, 1)] [Array([1, 2, 3]), Array([4, 5, 6]), Array([7, 8, 9]), Array([10, 11, 12]), Array([13, 14, 15]), Array([16, 17, 18]), Array([19, 20, 21]), Array([22, 23, 24]), Array([25, 26, 27])] >>> [a for a in ArrayIter(A, 0, 2)] [Array([1, 4, 7]), Array([2, 5, 8]), Array([3, 6, 9]), Array([10, 13, 16]), Array([11, 14, 17]), Array([12, 15, 18]), Array([19, 22, 25]), Array([20, 23, 26]), Array([21, 24, 27])] >>> [a for a in ArrayIter(A, 0, 1, 2)] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27] >>> [a for a in ArrayIter(A, 0, 2, 1)] [1, 4, 7, 2, 5, 8, 3, 6, 9, 10, 13, 16, 11, 14, 17, 12, 15, 18, 19, 22, 25, 20, 23, 26, 21, 24, 27]
ArrayIter iterators support __len__, __getitem__, __setitem__ and __delitem__ methods, it can be used to set whole sub-arrays in any order (for instance rows or columns in MatrixN)
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> [a for a in ArrayIter(A, 0, 1)] [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> len(ArrayIter(A, 0, 1)) 9 >>> ArrayIter(A, 0, 1)[5:9] = [4, 3, 2, 1] >>> print A.formated() [[1, 2, 3], [4, 5, 4], [3, 2, 1]] >>> [a for a in ArrayIter(A, 1)] [Array([1, 4, 3]), Array([2, 5, 2]), Array([3, 4, 1])] >>> len(ArrayIter(A, 1)) 3 >>> ArrayIter(A, 1)[1] = [7, 8, 9] >>> print A.formated() [[1, 7, 3], [4, 8, 4], [3, 9, 1]] >>> ArrayIter(A, 0)[1] = 0 >>> print A.formated() [[1, 7, 3], [0, 0, 0], [3, 9, 1]]
- __delitem__(index)¶
it.__delitem__(index) <==> del it[index]
Note : if it is an ArrayIter built on Array a, it’s equivalent to del a[c] for c in it.toArrayCoords(index)
Warning : Do not use __delitem__ during iteration
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> [a for a in ArrayIter(A, 0, 1)] [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> del ArrayIter(A, 0, 1)[1] >>> [a for a in ArrayIter(A, 0, 1)] [4, 6, 7, 9] >>> print A.formated() [[4, 6], [7, 9]] >>> [a for a in ArrayIter(A, 1)] [Array([4, 7]), Array([6, 9])] >>> del ArrayIter(A, 1)[-1] >>> print A.formated() [[4], [7]]
- __getitem__(index)¶
it.__getitem__(index) <==> it[index]
Returns a single sub-Array or component corresponding to the iterator item designated by index, or an Array of values if index is a slice.
Note : if it is an ArrayIter built on Array a, it’s equivalent to a[c] for c in it.toArrayCoords(index)
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> [a for a in ArrayIter(A, 0, 1)] [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> ArrayIter(A, 0, 1)[4] 5 >>> ArrayIter(A, 0, 1)[0:6] Array([1, 2, 3, 4, 5, 6]) >>> [a for a in ArrayIter(A, 1)] [Array([1, 4, 7]), Array([2, 5, 8]), Array([3, 6, 9])] >>> ArrayIter(A, 1)[1] Array([2, 5, 8]) >>> ArrayIter(A, 1)[1, 0] 2 >>> print ArrayIter(A, 1)[0:2, 0:2].formated() [[1, 4], [2, 5]] >>> print A.transpose()[0:2, 0:2].formated() [[1, 4], [2, 5]]
- __setitem__(index, value)¶
it.__setitem__(index, value) <==> it[index] = value
Returns a single sub-Array or component corresponding to the iterator item item, or an Array of values if index is a slice.
Note : if it is an ArrayIter built on Array a, it’s equivalent to a[c]=value for c in it.toArrayCoords(index) or a[c] = value[i] for i, c in enumerate(it.toArrayCoords(index)) if an iterable of values of suitable shapes was provided.
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> [a for a in ArrayIter(A, 0, 1)] [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> ArrayIter(A, 0, 1)[4] = 10 >>> [a for a in ArrayIter(A, 0, 1)] [1, 2, 3, 4, 10, 6, 7, 8, 9] >>> print A.formated() [[1, 2, 3], [4, 10, 6], [7, 8, 9]] >>> ArrayIter(A, 0, 1)[0:3] = 1 >>> [a for a in ArrayIter(A, 0, 1)] [1, 1, 1, 4, 10, 6, 7, 8, 9] >>> print A.formated() [[1, 1, 1], [4, 10, 6], [7, 8, 9]] >>> ArrayIter(A, 0, 1)[5:9] = [4, 3, 2, 1] >>> [a for a in ArrayIter(A, 0, 1)] [1, 1, 1, 4, 10, 4, 3, 2, 1] >>> print A.formated() [[1, 1, 1], [4, 10, 4], [3, 2, 1]] >>> [a for a in ArrayIter(A, 1)] [Array([1, 4, 3]), Array([1, 10, 2]), Array([1, 4, 1])] >>> ArrayIter(A, 1)[1] Array([1, 10, 2]) >>> ArrayIter(A, 1)[1, 1] = 5 >>> print A.formated() [[1, 1, 1], [4, 5, 4], [3, 2, 1]] >>> ArrayIter(A, 1)[-1] = [7, 8, 9] >>> print A.formated() [[1, 1, 7], [4, 5, 8], [3, 2, 9]] >>> ArrayIter(A, 0)[1] = 0 >>> print A.formated() [[1, 1, 7], [0, 0, 0], [3, 2, 9]]
- next() → the next value, or raise StopIteration¶
- toArrayCoords(index, default=None)¶
it.toArrayCoords(index, default=None) –> list or tuple
Converts an iterator item index (item of number index in the iterator) for that Array iterator to a tuple of axis coordinates for that Array, returns a single coordinates tuple or a list of coordinate tuples if index was a slice. If index is a multi-index (a tuple), the first element if index is checked against the iterator and the remaining elements are considered indices on the iterated sub-array (s).
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> [a for a in ArrayIter(A, 0, 1)] [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> it = ArrayIter(A, 0, 1) >>> it[4] 5 >>> it.toArrayCoords(4) (1, 1) >>> A[1, 1] 5 >>> it[1:4] Array([2, 3, 4]) >>> it.toArrayCoords(slice(1, 4)) [(0, 1), (0, 2), (1, 0)]
>>> [a for a in ArrayIter(A, 1)] [Array([1, 4, 7]), Array([2, 5, 8]), Array([3, 6, 9])] >>> it = ArrayIter(A, 1) >>> it[0] Array([1, 4, 7]) >>> it.toArrayCoords(0) (None, 0) >>> it.toArrayCoords(0, default=slice(None)) (slice(None, None, None), 0) >>> A[:, 0] Array([1, 4, 7]) >>> it.toArrayCoords((0, 1)) (1, 0) >>> it[0, 1] 4 >>> A[1, 0] 4