Python mảng tĩnh
Trong bài viết này, chúng ta sẽ tìm hiểu mảng trong lập trình python là gì và cách khởi tạo mảng trong python như thế nào? . Ngoài ra, chúng ta sẽ xem một ví dụ về từng phương thức cùng với cú pháp của nó sẽ được tuân theo và đầu ra của ví dụ đã cho. Vì vậy, hãy bắt đầu Show
Mảng trong Python là gì?Mảng là một cấu trúc dữ liệu trong lập trình python chứa số phần tử cố định và các phần tử này phải có cùng kiểu dữ liệu. Ý tưởng chính đằng sau việc sử dụng một mảng lưu trữ nhiều phần tử cùng loại. Hầu hết cấu trúc dữ liệu sử dụng một mảng để thực hiện thuật toán của họ. Có hai phần quan trọng của mảng
Các phần tử này phân bổ các vị trí bộ nhớ liền kề cho phép dễ dàng sửa đổi dữ liệu. Trong ngôn ngữ python, trước khi sử dụng mảng chúng ta cần khai báo module tên là “array” bằng từ khóa “import” 3 cách để khởi tạo một mảng trong PythonĐể sử dụng mảng trong ngôn ngữ python có tổng cộng 3 cách khởi tạo. Chúng ta sẽ xem xét cả 3 cách khởi tạo một mảng trong python. Hãy để chúng tôi nghiên cứu từng cái một dưới đây Sử dụng vòng lặp for và Python range() HàmĐể khởi tạo một mảng với giá trị mặc định, chúng ta có thể sử dụng hàm for loop và range() trong ngôn ngữ python cú pháp. [giá trị cho phần tử trong phạm vi (num)] Hàm range() trong Python lấy một số làm đối số và trả về một dãy số bắt đầu từ 0 và kết thúc bởi một số cụ thể, mỗi lần tăng thêm 1 Ngôn ngữ Python cho vòng lặp sẽ đặt giá trị 0 (giá trị mặc định) cho mọi mục bên trong mảng giữa phạm vi được chỉ định trong hàm phạm vi () Ví dụ array=[] array = [0 for i in range(3)] print(array) Đầu ra của đoạn mã trên sẽ như hình dưới đây [0, 0, 0] Khởi tạo mảng bằng mô-đun python NumPyNgôn ngữ Python có nhiều thư viện và chức năng sẵn có giúp công việc của chúng ta dễ dàng và đơn giản hơn so với các ngôn ngữ lập trình khác. Mô-đun NumPy là một trong số đó. Mô-đun NumPy có thể được sử dụng để khởi tạo mảng và thao tác dữ liệu được lưu trữ trong đó. Con số. Hàm empty() của mô-đun NumPy tạo một mảng có kích thước xác định với giá trị mặc định=”None” cú pháp. cục mịch. trống (kích thước, dtype = đối tượng) Ví dụ import numpy as np array = np.empty(5, dtype=object) print(array) Đầu ra của đoạn mã trên sẽ như hình dưới đây [Không Không Không Không Không Không Không] Các phương thức trực tiếp để khởi tạo một mảngTrong ngôn ngữ python, chúng ta có thể khởi tạo trực tiếp các phần tử bên trong một mảng bằng phương thức bên dưới cú pháp. tên mảng = [giá trị mặc định]*kích thước Ví dụ arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string) Đầu ra của mã trên là như hình dưới đây [1, 1, 1] ['Đ', 'Đ', 'Đ'] Phần kết luậnDo đó, mảng được sử dụng để lưu trữ các phần tử có cùng kiểu dữ liệu và trên đây là một số phương thức được sử dụng để tạo hoặc khởi tạo mảng trong lập trình python. Hơn nữa, chúng ta có thể thực hiện nhiều phép toán và sửa đổi như thêm, xóa hoặc cập nhật một phần tử bên trong mảng với cú pháp thích hợp để tuân theo. Tôi hy vọng cuối cùng bạn đã học được cách khởi tạo một mảng trong python Mảng trong Python có thể được tạo bằng cách nhập mô-đun mảng. array(data_type, value_list) được sử dụng để tạo một mảng với kiểu dữ liệu và danh sách giá trị được chỉ định trong các đối số của nó. Python3arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)86 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)87
arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)88 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.40 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.41
Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.42 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.43 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.45 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.46 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.47 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.48 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 import numpy as np array = np.empty(5, dtype=object) print(array)30 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 import numpy as np array = np.empty(5, dtype=object) print(array)32 import numpy as np array = np.empty(5, dtype=object) print(array)33
import numpy as np array = np.empty(5, dtype=object) print(array)34 import numpy as np array = np.empty(5, dtype=object) print(array)35 import numpy as np array = np.empty(5, dtype=object) print(array)36____137 import numpy as np array = np.empty(5, dtype=object) print(array)38 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)30 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)31 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)32 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)33 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)34 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)35 import numpy as np array = np.empty(5, dtype=object) print(array)36 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)37 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 import numpy as np array = np.empty(5, dtype=object) print(array)32 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.430 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.431 import numpy as np array = np.empty(5, dtype=object) print(array)35 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.433 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)30 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)31 import numpy as np array = np.empty(5, dtype=object) print(array)35 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.438
Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.439 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.430 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.45 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.433 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.47 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.435 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.437 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.439 import numpy as np array = np.empty(5, dtype=object) print(array)33
import numpy as np array = np.empty(5, dtype=object) print(array)34 import numpy as np array = np.empty(5, dtype=object) print(array)35 import numpy as np array = np.empty(5, dtype=object) print(array)36____137 import numpy as np array = np.empty(5, dtype=object) print(array)38 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)30 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)31 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)32 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)33 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)34 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)35 import numpy as np array = np.empty(5, dtype=object) print(array)36 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)37 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 import numpy as np array = np.empty(5, dtype=object) print(array)32 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.430 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.431 import numpy as np array = np.empty(5, dtype=object) print(array)35 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)870 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)30 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)31 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)874 đầu ra. Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.46 Sự phức tạp để tạo mảngThời gian phức tạp. Ô(1) Không gian phụ trợ. Trên) Một số kiểu dữ liệu được đề cập dưới đây sẽ giúp tạo ra một mảng các kiểu dữ liệu khác nhau. Thêm các phần tử vào một mảngCó thể thêm các phần tử vào Mảng bằng cách sử dụng hàm insert() tích hợp. Chèn được sử dụng để chèn một hoặc nhiều phần tử dữ liệu vào một mảng. Dựa trên yêu cầu, một phần tử mới có thể được thêm vào đầu, cuối hoặc bất kỳ chỉ mục nào của mảng. append() cũng được sử dụng để thêm giá trị được đề cập trong các đối số của nó vào cuối mảng. Python3arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)86 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)876
arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)88 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.40 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.41
arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)880 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.43 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.45 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.46 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.47 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.48 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 import numpy as np array = np.empty(5, dtype=object) print(array)30 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 import numpy as np array = np.empty(5, dtype=object) print(array)32 import numpy as np array = np.empty(5, dtype=object) print(array)33
import numpy as np array = np.empty(5, dtype=object) print(array)35 import numpy as np array = np.empty(5, dtype=object) print(array)36____304 import numpy as np array = np.empty(5, dtype=object) print(array)38 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)30 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)31 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)32 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)33 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)34 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)35 import numpy as np array = np.empty(5, dtype=object) print(array)36 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)37 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 import numpy as np array = np.empty(5, dtype=object) print(array)32 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.430 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.431 import numpy as np array = np.empty(5, dtype=object) print(array)35 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.433 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)30 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)31 import numpy as np array = np.empty(5, dtype=object) print(array)35 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.438
Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.426 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.427 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.428 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.48____39 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.431 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)31
import numpy as np array = np.empty(5, dtype=object) print(array)35 import numpy as np array = np.empty(5, dtype=object) print(array)36____335 import numpy as np array = np.empty(5, dtype=object) print(array)38 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)30 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)31 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)32 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)33 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)34 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.443 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.431 import numpy as np array = np.empty(5, dtype=object) print(array)35 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.446 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)30 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)31 import numpy as np array = np.empty(5, dtype=object) print(array)35 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.438
Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.452 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.430 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.45 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.433 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.47 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.435 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.437 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.439 import numpy as np array = np.empty(5, dtype=object) print(array)33
import numpy as np array = np.empty(5, dtype=object) print(array)35 import numpy as np array = np.empty(5, dtype=object) print(array)36____304 import numpy as np array = np.empty(5, dtype=object) print(array)38 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)30 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)31 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)32 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)33 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)34 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)35 import numpy as np array = np.empty(5, dtype=object) print(array)36 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)37 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 import numpy as np array = np.empty(5, dtype=object) print(array)32 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.430 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.431 import numpy as np array = np.empty(5, dtype=object) print(array)35 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)870 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)30 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)31 import numpy as np array = np.empty(5, dtype=object) print(array)35 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.438
Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.488 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.489 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.490 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)31
import numpy as np array = np.empty(5, dtype=object) print(array)35 import numpy as np array = np.empty(5, dtype=object) print(array)36____335 import numpy as np array = np.empty(5, dtype=object) print(array)38 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)30 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)31 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)32 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)33 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)34 import numpy as np array = np.empty(5, dtype=object) print(array)302 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.431 import numpy as np array = np.empty(5, dtype=object) print(array)35 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.446 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)30 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)31 import numpy as np array = np.empty(5, dtype=object) print(array)35 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.438 đầu ra. Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.4 Sự phức tạp khi thêm các phần tử vào MảngThời gian phức tạp. O(1)/O(n) ( O(1) – để chèn các phần tử vào cuối mảng, O(n) – để chèn các phần tử vào đầu mảng và vào toàn bộ mảng Không gian phụ trợ. Ô(1) Truy cập các phần tử từ MảngĐể truy cập các mục mảng tham khảo số chỉ mục. Sử dụng toán tử chỉ mục [ ] để truy cập một mục trong mảng. Chỉ số phải là một số nguyên. Python3arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)86 import numpy as np array = np.empty(5, dtype=object) print(array)312
import numpy as np array = np.empty(5, dtype=object) print(array)313 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.40 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.41
arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)880 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.43__ Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.45 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.46 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.47 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.48 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 import numpy as np array = np.empty(5, dtype=object) print(array)30 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 import numpy as np array = np.empty(5, dtype=object) print(array)32 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.431 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 import numpy as np array = np.empty(5, dtype=object) print(array)330 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 import numpy as np array = np.empty(5, dtype=object) print(array)332 import numpy as np array = np.empty(5, dtype=object) print(array)33
import numpy as np array = np.empty(5, dtype=object) print(array)334 import numpy as np array = np.empty(5, dtype=object) print(array)35 import numpy as np array = np.empty(5, dtype=object) print(array)36 import numpy as np array = np.empty(5, dtype=object) print(array)337 import numpy as np array = np.empty(5, dtype=object) print(array)338 arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)37 import numpy as np array = np.empty(5, dtype=object) print(array)33
import numpy as np array = np.empty(5, dtype=object) print(array)334 import numpy as np array = np.empty(5, dtype=object) print(array)35 import numpy as np array = np.empty(5, dtype=object) print(array)36 import numpy as np array = np.empty(5, dtype=object) print(array)337 import numpy as np array = np.empty(5, dtype=object) print(array)338 import numpy as np array = np.empty(5, dtype=object) print(array)32 import numpy as np array = np.empty(5, dtype=object) print(array)33
Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.452 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.430 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.45 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.433 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.47 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.435 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.437 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.439 import numpy as np array = np.empty(5, dtype=object) print(array)33
import numpy as np array = np.empty(5, dtype=object) print(array)334 import numpy as np array = np.empty(5, dtype=object) print(array)35 import numpy as np array = np.empty(5, dtype=object) print(array)36____1337 import numpy as np array = np.empty(5, dtype=object) print(array)364 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.48 import numpy as np array = np.empty(5, dtype=object) print(array)33
import numpy as np array = np.empty(5, dtype=object) print(array)334 import numpy as np array = np.empty(5, dtype=object) print(array)35 import numpy as np array = np.empty(5, dtype=object) print(array)36 import numpy as np array = np.empty(5, dtype=object) print(array)337 import numpy as np array = np.empty(5, dtype=object) print(array)364 import numpy as np array = np.empty(5, dtype=object) print(array)30 import numpy as np array = np.empty(5, dtype=object) print(array)33 đầu ra. import numpy as np array = np.empty(5, dtype=object) print(array)3 Sự phức tạp khi truy cập các phần tử trong MảngThời gian phức tạp. Ô(1) Không gian phụ trợ. Ô(1) Loại bỏ các phần tử khỏi mảngCó thể xóa các phần tử khỏi mảng bằng cách sử dụng hàm remove() tích hợp nhưng sẽ phát sinh Lỗi nếu phần tử không tồn tại trong tập hợp. Phương thức Remove() chỉ xóa một phần tử tại một thời điểm, để xóa phạm vi phần tử, trình vòng lặp được sử dụng. hàm pop() cũng có thể được sử dụng để xóa và trả về một phần tử khỏi mảng, nhưng theo mặc định, hàm này chỉ xóa phần tử cuối cùng của mảng, để xóa phần tử khỏi một vị trí cụ thể của mảng, chỉ số của phần tử được truyền dưới dạng . Python3arr_number = [1] * 3 print(arr_number) arr_string = ['D'] * 3 print(arr_string)86 import numpy as np array = np.empty(5, dtype=object) print(array)375 import numpy as np array = np.empty(5, dtype=object) print(array)376 import numpy as np array = np.empty(5, dtype=object) print(array)377 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.40 import numpy as np array = np.empty(5, dtype=object) print(array)379 import numpy as np array = np.empty(5, dtype=object) print(array)376 import numpy as np array = np.empty(5, dtype=object) print(array)381 import numpy as np array = np.empty(5, dtype=object) print(array)382 import numpy as np array = np.empty(5, dtype=object) print(array)383 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.44 import numpy as np array = np.empty(5, dtype=object) print(array)385_______36 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.47 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.48 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 import numpy as np array = np.empty(5, dtype=object) print(array)30 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 import numpy as np array = np.empty(5, dtype=object) print(array)32 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.48 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 import numpy as np array = np.empty(5, dtype=object) print(array)30 Array before insertion : 1 2 3 Array after insertion : 1 4 2 3 Array before insertion : 2.5 3.2 3.3 Array after insertion : 2.5 3.2 3.3 4.49 import numpy as np array = np.empty(5, dtype=object) print(array)330 import numpy as np array = np.empty(5, dtype=object) print(array)33 Mảng tĩnh và mảng động Python là gì?Mảng động là một cấu trúc dữ liệu danh sách có kích thước thay đổi, truy cập ngẫu nhiên cho phép thêm hoặc bớt các phần tử . Nó được cung cấp với các thư viện tiêu chuẩn trong nhiều ngôn ngữ lập trình hiện đại. Mảng động vượt qua giới hạn của mảng tĩnh, có dung lượng cố định cần được chỉ định khi phân bổ.
Mảng tĩnh với ví dụ là gì?Mảng tĩnh là cấu trúc dữ liệu có kích thước cố định . Chúng ta hãy xem một ví dụ về mảng tĩnh trong C#. Đây là một mảng chuỗi tĩnh. Dữ liệu vẫn giữ nguyên ở đây tôi. e. fixed − static string[] _fruits = new string[] { "apple", "xoài" };
Bạn có thể tạo một mảng tĩnh không?Các mảng được khai báo tĩnh được cấp phát bộ nhớ tại thời điểm biên dịch và kích thước của chúng là cố định, i. e. , không thể thay đổi sau này. Chúng có thể được khởi tạo theo cách tương tự như Java . Ví dụ hai mảng int được khai báo, một mảng được khởi tạo, một mảng không. Mảng nhiều chiều tĩnh được khai báo với nhiều chiều.
Mảng NumPy tĩnh hay động?Một mảng Python là mảng động và bạn có thể nối thêm các phần tử mới và xóa các phần tử hiện có. Mảng NumPy giống phiên bản hướng đối tượng của mảng C hoặc C++ truyền thống . Bạn có thể tạo các mảng NumPy bằng nhiều loại dữ liệu từ int8, uint8, float64, bool và cho đến complex128. |