str2d.Str2D.insert

Str2D.insert(indices_or_sections, axis=0, sep=' ')[source]

insert is a wrapper around the split method and is used to visually insert a separator between the split data. The sep is the separator that will be added between the split data.

If the sep is not an empty string, it will be added between the concatenated data. If the sep is an empty string, the data will be concatenated without any separation and wll resemble the original data. This would be silly to do but it’s a good way to indicate that the method is doing what it’s supposed to do.

The actual intention is to use a separator to visually separate the data.

The ary being passed to np.split is the structured array data.

Parameters:
  • indices_or_sections (int or 1-D array) –

    If indices_or_sections is an integer, N, the array will be divided into N equal arrays along axis. If such a split is not possible, an error is raised.

    If indices_or_sections is a 1-D array of sorted integers, the entries indicate where along axis the array is split. For example, [2, 3] would, for axis=0, result in

    • ary[:2]

    • ary[2:3]

    • ary[3:]

    If an index exceeds the dimension of the array along axis, an empty sub-array is returned correspondingly.

  • axis (int, optional) – The axis along which to split, default is 0.

  • sep (str, optional) – The separator to insert between the split data, by default ‘ ‘.

Returns:

A new Str2D object with the split data and separator.

Return type:

Str2D

See also

split

Split the data along an axis.

Examples

Let’s create an instance of Str2D and assign it to the variable a and then

from str2d import Str2D

a = Str2D('abcdef\nghijkl\nmnopqr\nstuvwx')
a
abcdef
ghijkl
mnopqr
stuvwx

Let’s insert 4 equal parts along the vertical axis with a separator of ‘-’ followed by an insert of 3 equal parts along the horizontal axis with a separator of ‘|’.

a.insert(4, sep='-').insert(3, axis=1, sep='|')
ab|cd|ef
--|--|--
gh|ij|kl
--|--|--
mn|op|qr
--|--|--
st|uv|wx