Function adas.array2strlist
def array2strlist(array, fmt=None, num_col=8)
-
Convert a numpy array to a list of strings according to a format string.
Parameters
array
:numpy array
- numerical data to be converted.
fmt
:str
- numeric format for output, one of d, f, e or E.
num_col
:int
- required number of colums in output, defaults to 8.
Returns
value
:list, str
- list of strings.
Notes
A design pattern in ADAS is to assemble the contents of a file in a list of strings. This routine takes a numpy numerical array and generates a list of string according to a user supplied format and number of columns.
Version History
-
Martin O'Mullane, 25-07-2018
- First version
-
Martin O'Mullane, 26-07-2018
- For dtypes which are not float or integer set array_kind to None to fall back to default formatting.
-
Martin O'Mullane, 27-07-2018
- numpy.array2string has a default limit of 1000 array elements where it summarizes rather than outputs all the elements. For numpy versions up to v1.13, printoptions must be changed to remove this limit. For v1.14 numpy.array2string has extra keyword options.
-
Martin O'Mullane, 12-11-2019
- set legacy='1.13' in array2string rather than True which is not a valid option.
-
Martin O'Mullane, 26-11-2021
- change internal variable name 'all' to 'lines'.
- change options to numpy.array2string to ensure that the supplied format statement acts as expected and does not add unexpected padding spaces.
Example
>>> import adas as adas >>> import numpy as np >>> result = np.array([2.3, 5.5, 8.5, 12.0, 41.2]) >>> lines = adas.array2strlist(result, fmt='{:9.2e}', num_col=3) >>> lines [' 2.30e+00 5.50e+00 8.50e+00', ' 1.20e+01 4.12e+01'] >>> for l in lines: print(l) 2.30e+00 5.50e+00 8.50e+00 1.20e+01 4.12e+01 >>> lines = adas.array2strlist(result, fmt='{:9.2f}', num_col=3) >>> for l in lines: print(l) 2.30 5.50 8.50 12.00 41.20