str2d.Str2D.split
- Str2D.split(indices_or_sections, axis=0) List[Str2D] [source]
split is analogous to np.split and splits the data along an axis. It’ll return a list of Str2D objects split along the specified axis at the specified indices.
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, foraxis=0
, result inary[: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.
- Returns:
A list of Str2D applied to the split data.
- Return type:
List[Str2D]
- Raises:
ValueError – If indices_or_sections is given as an integer, but a split does not result in equal division.
See also
insert
Insert a separator between the split data.
Examples
Let’s create an instance of Str2D and assign it to the variable a and then split it.
from str2d import Str2D a = Str2D('abcdef\nghijkl\nmnopqr\nstuvwx') a
abcdef ghijkl mnopqr stuvwx
We can split the data into 3 equal parts along the vertical axis. We’ll use the complementary insert method to join the split data back together to help visualize the split.
Two equal parts along the vertical axis. .. testcode:
a.split(2)
[abcdef ghijkl, mnopqr stuvwx]
Using the insert method to join the split data back together.
a.insert(2)
abcdef ghijkl mnopqr stuvwx
3 equal parts along the horizontal axis. Remember that we’re using the insert method to show the data. Use the split method to get the split data as a list.
a.insert(3, axis=1)
ab cd ef gh ij kl mn op qr st uv wx
Split the data at first and fifth columns.
a.insert([1, 5], axis=1)
a bcde f g hijk l m nopq r s tuvw x