Thao tác với danh sách là một kỹ năng quan trọng đối với bất kỳ Pythonista mới chớm nở nào. 9 trên 10 lần, việc thêm dữ liệu vào danh sách được thực hiện bằng phương thức append[], phương thức này sẽ thêm giá trị vào cuối danh sách. Tuy nhiên, đôi khi chúng ta cần thêm một giá trị vào giữa danh sách
Giả sử chúng ta cần ghi tuổi của 5 học sinh. Thay vì tạo 5 biến riêng biệt, chúng ta chỉ cần tạo một danh sách
Tạo một danh sách Python
Một danh sách được tạo bằng Python bằng cách đặt các mục bên trong
languages = ["Python", "Swift", "C++"]
# access item at index 0
print[languages[0]] # Python
# access item at index 2
print[languages[2]] # C++
5, được phân tách bằng dấu phẩy. Ví dụ,# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
Ở đây, chúng tôi đã tạo một danh sách có tên số với 3 mục số nguyên
Một danh sách có thể có bất kỳ số lượng mục nào và chúng có thể thuộc các loại khác nhau [số nguyên, số float, chuỗi, v.v. ]. Ví dụ,
# empty list
my_list = []
# list with mixed data types
my_list = [1, "Hello", 3.4]
Truy cập các phần tử danh sách Python
Trong Python, mỗi mục trong danh sách được liên kết với một số. Số được gọi là chỉ mục danh sách
Chúng ta có thể truy cập các phần tử của một mảng bằng cách sử dụng số chỉ mục [0, 1, 2…]. Ví dụ,
languages = ["Python", "Swift", "C++"]
# access item at index 0
print[languages[0]] # Python
# access item at index 2
print[languages[2]] # C++
Trong ví dụ trên, chúng tôi đã sử dụng khả năng hiểu danh sách để tạo danh sách với mỗi mục được tăng theo lũy thừa của 2. Chú ý mã,
Chuyển đến nội dung chínhGiới thiệu về Python
Nắm vững kiến thức cơ bản về phân tích dữ liệu với Python chỉ trong bốn giờ. Khóa học trực tuyến này sẽ giới thiệu giao diện Python và khám phá các gói phổ biến
Python trung cấp
Nâng cao kỹ năng khoa học dữ liệu của bạn bằng cách tạo trực quan hóa bằng Matplotlib và thao tác với DataFrames bằng gấu trúcCó liên quan
Các tài nguyên hàng đầu năm 2022 để nâng cao kỹ năng dữ liệu của bạn
Nhận quyền truy cập vào các tài nguyên hoạt động hiệu quả nhất của chúng tôi từ năm 2022, bao gồm hội thảo trên web, bài đăng trên blog, sách trắng, bảng gian lận, hướng dẫn và bài viết, tất cả đều được thiết kế để giúp bạn nâng cao kỹ năng dữ liệu và mở rộng quy mô văn hóa dữ liệu của tổ chức bạn. Bắt đầu học hỏi và phát triển kiến thức chuyên môn về dữ liệu của bạn ngay hôm nayDữ liệu văn bản trong Python Cheat Sheet
Chào mừng bạn đến với bảng gian lận của chúng tôi để làm việc với dữ liệu văn bản trong Python. Chúng tôi đã biên soạn một danh sách các hàm và gói hữu ích nhất để dọn dẹp, xử lý và phân tích dữ liệu văn bản trong Python, cùng với các ví dụ và giải thích rõ ràng, vì vậy bạn sẽ có mọi thứ cần biết về cách làm việc với dữ liệu văn bản trong Python.Hướng dẫn về tập hợp và lý thuyết tập hợp trong Python
Tìm hiểu về bộ Python. chúng là gì, cách tạo chúng, khi nào sử dụng chúng, các chức năng tích hợp và mối quan hệ của chúng với các hoạt động lý thuyết thiết lậpHướng dẫn về gấu trúc. Khung dữ liệu trong Python
Khám phá phân tích dữ liệu với Python. Pandas DataFrames giúp thao tác dữ liệu của bạn dễ dàng, từ việc chọn hoặc thay thế các cột và chỉ mục để định hình lại dữ liệu của bạnXem ThêmXem ThêmChúng tôi thường thực hiện nhiệm vụ lấy các giá trị chỉ mục nhất định và gán các biến từ chúng. Cách tiếp cận chung mà chúng tôi tuân theo là trích xuất từng phần tử danh sách theo chỉ mục của nó và sau đó gán nó cho các biến. Cách tiếp cận này yêu cầu nhiều dòng mã hơn. Hãy thảo luận về một số cách nhất định để thực hiện nhiệm vụ này một cách gọn nhẹ để cải thiện khả năng đọc.
Phương pháp số 1. Sử dụng khả năng hiểu danh sách Bằng cách sử dụng khả năng hiểu danh sách, người ta có thể hoàn thành nhiệm vụ này một cách dễ dàng và chỉ trong một dòng. Chúng tôi chạy một vòng lặp cho các chỉ số cụ thể trong RHS và gán chúng cho các biến bắt buộc.
Python3
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
15# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
16# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
17
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
18# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
19# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
20 # A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
21_______40The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 32
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31______44
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 36
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 30
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 32
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 33
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 34
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 35
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 36
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 37
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 39
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
150
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
17# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
16# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
153# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
20 # A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
155# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
156 # A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
157# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
158 # A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
159The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 30
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 30
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 34
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
165
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
166The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 33
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
168The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 39
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
171The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
173# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
174The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 39
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
177The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
179# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
174The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 39
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
183đầu ra.
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 3
Phương pháp #2. Sử dụng chức năng itemgetter[] itemgetter cũng có thể được sử dụng để thực hiện tác vụ cụ thể này. Hàm này chấp nhận các giá trị chỉ mục và vùng chứa mà nó đang làm việc và gán cho các biến.
Python3
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
15# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
16# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
186________ 1187 ________ 1188 ________ 1189 ________ 1190
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
18# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
19# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
20 # A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
21_______40The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 32
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31______44
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 36
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 30
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 32
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 33
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 34
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 35
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 36
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 37
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 39
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
150
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
216# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
16# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
153# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
20 The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 300
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 30
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 30
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 34
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 306
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
166The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 33
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
168The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 39
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
171The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
173# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
174The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 39
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
177The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
179# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
174The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 39
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
183đầu ra.
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 3
Phương pháp #3. Sử dụng itertools. hàm nén nén[] chấp nhận các giá trị boolean tương ứng với mỗi chỉ mục là True nếu nó phải được gán cho biến và Sai thì nó không được sử dụng trong việc gán biến.
Python3
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
15# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
16The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 327
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
187 The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 329____1189
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 331
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
18# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
19# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
20 # A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
21_______40The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 32
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31______44
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 36
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 30
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 32
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 33
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 34
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 35
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 36
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 37
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 39
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
150
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 357
# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
16# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
153# A list with 3 integers
numbers = [1, 2, 5]
print[numbers]
# Output: [1, 2, 5]
20 The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 361
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 362
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 30
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 362
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 30
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 362
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 30
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 362
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 375