Hướng dẫn typecast array python

Hướng dẫn typecast array python

Nội dung chính

  • Using numpy.asarray() method to convert list to an array
  • np.array vs np.asarray
  • How do you convert an array?
  • How do you convert an array to a number in Python?
  • How do you convert an array to a DataFrame in Python?
  • How we can convert the NumPy array?

Python list is a linear data structure that can hold heterogeneous elements. Unfortunately, Python does not have a built-in array data type, but we can use the numpy library to create and modify arrays.

To create an array in Python, use the numpy library. To install numpy in your system, type the following command.

python3 -m pip install numpy

To create a numpy array, use the numpy.array() function. To create an empty array, use the numpy empty() function.

During programming, there will be instances when you need to convert existing lists to arrays to perform certain operations on them. In this example, we will see how to convert lists to arrays in Python.

To convert a list to array in Python, use the np.array() method. The np.array() is a numpy library function that takes a list as an argument and returns an array containing all the list elements.

import numpy as np

elon_list = [11, 21, 19, 18, 29]
elon_array = np.array(elon_list)

print(elon_array)
print(type(elon_array))

Output

[11 21 19 18 29]

In this example, we defined a list, which we converted into an array using the np.array() function and printed the array and its data type. To check variable data type in Python, use the type() function.

Using numpy.asarray() method to convert list to an array

The np.asarray() is a numpy library function that takes a list as an argument converts it into an array, and returns it. As per the definition of the numpy.asarray() function, it calls the numpy.array() function inside itself.

So behind the scenes, np.asarray() function calls the np.array() function.

def asarray(a, dtype=None, order=None):
    return array(a, dtype, copy=False, order=order)

The main difference between numpy.array() and numpy.asarray() is that the copy flag is False in the case of numpy.asarray(), and True (by default) in the case of numpy.array().

import numpy as np

elon_list = [11, 21, 19, 18, 29]
elon_array = np.asarray(elon_list)

print(elon_array)
print(type(elon_array))

Output

[11 21 19 18 29]

np.array vs np.asarray

The main difference between np.array() and np.asarray() is that np.array() will create a duplicate of the original object and np.asarray() will follow the changes in the original object.

For example, when a copy of the array is made using np.asarray(), the modifications made in one array would be reflected in the other array but don’t display the changes in the list from which an array is made. In the case of np.array(), this doesn’t happen.

That is it for converting the list to an array in Python.

See also

Python list to a tuple

Python list to string

Python list to dataframe

Python list to json

Python set to list

A list in Python is a linear data structure that can hold heterogeneous elements they do not require to be declared and are flexible to shrink and grow. On the other hand, an array is a data structure which can hold homogeneous elements, arrays are implemented in Python using the NumPy library. Arrays require less memory than list.

The similarity between an array and a list is that the elements of both array and a list can be identified by its index value.
In Python lists can be converted to arrays by using two methods from the NumPy library: 
 

  • Using numpy.array() 

Python3

import numpy

lst = [1, 7, 0, 6, 2, 5, 6]

arr = numpy.array(lst)

print ("List: ", lst)

print ("Array: ", arr)

Output: 

List:  [1, 7, 0, 6, 2, 5, 6]
Array:  [1 7 0 6 2 5 6]
  • Using numpy.asarray() 

Python3

import numpy

lst = [1, 7, 0, 6, 2, 5, 6]

arr = numpy.asarray(lst)

print ("List:", lst)

print ("Array: ", arr)

Output: 

List:  [1, 7, 0, 6, 2, 5, 6]
Array:  [1 7 0 6 2 5 6]

The vital difference between the above two methods is that numpy.array() will make a duplicate of the original object and numpy.asarray() would mirror the changes in the original object. i.e :

When a copy of the array is made by using numpy.asarray(), the changes made in one array would be reflected in the other array also but doesn’t show the changes in the list by which if the array is made. However, this doesn’t happen with numpy.array().

Python3

import numpy

lst = [1, 7, 0, 6, 2, 5, 6]

arr = numpy.asarray(lst)

print ("List:", lst)

print ("arr: ", arr)

arr1 = numpy.asarray(arr)

print("arr1: " , arr1)

arr1[3] = 23

print("lst: " , lst)

print("arr: " , arr)

print("arr1: " , arr1)

Output :

List: [1, 7, 0, 6, 2, 5, 6]
arr:  [1 7 0 6 2 5 6]
arr1:  [1 7 0 6 2 5 6]
lst:  [1, 7, 0, 6, 2, 5, 6]
arr:  [ 1  7  0 23  2  5  6]
arr1:  [ 1  7  0 23  2  5  6]

In “arr” and “arr1” the change is visible at index 3 but not in 1st.


How do you convert an array?

Get the Array to be converted..

Create an empty List..

Add the array into the List by passing it as the parameter to the Collections. addAll() method..

Return the formed List..

How do you convert an array to a number in Python?

How to convert a float array to an integer array in python ?.

Using the numpy function astype..

Round the numbers before converting them in integer..

Truncate the numbers first..

Round to the nearest bigger integer..

Round to the nearest smaller integer..

How do you convert an array to a DataFrame in Python?

How do you convert an array to a DataFrame in Python? To convert an array to a dataframe with Python you need to 1) have your NumPy array (e.g., np_array), and 2) use the pd. DataFrame() constructor like this: df = pd. DataFrame(np_array, columns=['Column1', 'Column2']) .

How we can convert the NumPy array?

To convert a Python list to a NumPy array, use either of the following two methods:.

The np. array() function that takes an iterable and returns a NumPy array creating a new data structure in memory..

The np. asarray() function that takes an iterable as argument and converts it to the array. The difference to np..