numpy.ascontiguousarray() in Python
The ascontiguousarray() returns a contiguous array (ndim>= 1) in memory (C order).
Syntax
1 2 3 |
numpy.ascontiguousarray(a, dtype=None) |
Parameter
arr : This parameter includes the input data, in any form that can be converted to an array. This includes lists, tuples, lists of tuples, tuples of tuples, tuples of lists etc.
dtype: It is an optional parameter. It depicts the data type of returned array, and by default, it is a float.
Return
This function returns a contiguous array (ndarray) of same shape and content as ‘arr’, with type specified in ‘dtype’.
Example 1
1 2 3 4 5 6 7 8 9 |
# Python Programming giving an example for # numpy.asanyarray() function importnumpy as numpy inp_list = [500, 1000, 1500, 2000] print ("Input list : ", inp_list) arr = numpy.ascontiguousarray(inp_list, dtype = numpy.float32) print ("output array from input list : ", arr) |
Output
1 2 3 4 |
Input list : [500, 1000, 1500, 2000] output array from input list : [ 500. 1000. 1500. 2000.] |
Example 2
1 2 3 4 5 6 7 8 9 |
# Python Programming giving an example for # numpy.asanyarray() function importnumpy as numpy inp_tuple = ([1, 6, 0], [8, 2, 46]) print ("Input touple : ", inp_tuple) arr = numpy.ascontiguousarray(inp_tuple, dtype = numpy.int32) print ("output array from input touple : ", arr) |
Output
1 2 3 4 5 |
Input touple : ([1, 6, 0], [8, 2, 46]) output array from input touple : [[ 1 6 0] [ 8 2 46]] |