1. Preface
In this chapter we will learn how to create an array from an existing array.
two.
Similar , but there are only three parameters, two fewer than .
(a, dtype = None, order = None)
Parameter description:
parameter | describe |
---|---|
a | Input parameters of any form can be, list, list tuple, tuple, tuple, tuple list, tuple, multidimensional array |
dtype | Data type, optional |
order | Optional, there are two options "C" and "F", which represent the order of storage elements in computer memory, respectively, row priority and column priority. |
Example
Convert the list to ndarray:
import numpy as np x = [1,2,3] a = (x) print (a)
The output result is:
[1 2 3]
Convert tuples to ndarray:
import numpy as np x = (1,2,3) a = (x) print (a)
The output result is:
[1 2 3]
Convert a list of tuples to ndarray:
import numpy as np x = [(1,2,3),(4,5)] a = (x) print (a)
The output result is:
[(1, 2, 3) (4, 5)]
The dtype parameter is set:
import numpy as np x = [1,2,3] a = (x, dtype = float) print (a)
The output result is:
[ 1. 2. 3.]
three.
Used to implement dynamic arrays.
Accept the buffer input parameter and read it in the form of a stream to convert it into an ndarray object.
(buffer, dtype = float, count = -1, offset = 0)
Notice:When buffer is a string, Python3 defaults to str is Unicode type, so it needs to be converted to bytestring and add b before the original str.
Parameter description:
parameter | describe |
---|---|
buffer | It can be any object and will be read in as a stream. |
dtype | Returns the data type of the array, optional |
count | The number of data read, default is -1, and all data is read. |
offset | The start position of read, default is 0. |
import numpy as np s = b'Hello World' a = (s, dtype = 'S1') print (a)
The output result is:
[b'H' b'e' b'l' b'l' b'o' b' ' b'W' b'o' b'r' b'l' b'd']
import numpy as np s = 'Hello World' a = (s, dtype = 'S1') print (a)
The output result is:
['H' 'e' 'l' 'l' 'o' ' ' 'W' 'o' 'r' 'l' 'd']
Four.
The method creates an ndarray object from an iterable object and returns a one-dimensional array.
(iterable, dtype, count=-1)
parameter | describe |
---|---|
iterable | Iterable object |
dtype | Returns the data type of the array |
count | The number of data to be read, default is -1, and all data is read |
import numpy as np # Create list objects using range functionlist=range(5) it=iter(list) # Create ndarray using iteratorx=(it, dtype=float) print(x)
The output result is:
[0. 1. 2. 3. 4.]
This is the article about NumPy creating arrays from existing arrays. For more related content on NumPy creating arrays from existing arrays, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!