Làm cách nào để sử dụng khả năng hiểu danh sách trong Python?

Chuyển đến nội dung chính

Giớ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úc

Có 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 nay

Dữ 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ập

Hướ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ạn

Xem ThêmXem Thêm

Và khả năng hiểu danh sách cung cấp cho bạn một cách để tạo danh sách trong khi viết mã thanh lịch hơn, dễ đọc

Trong bài viết thân thiện với người mới bắt đầu này, tôi sẽ cung cấp tổng quan về cách thức hoạt động của tính năng hiểu danh sách trong Python. Tôi cũng sẽ hiển thị nhiều ví dụ mã trên đường đi

Bắt đầu nào

Cách sử dụng vòng lặp
#initial list of numbers
numbers = [1,2,3,4,5,6]

#create a new,empty list to hold their squares
square_numbers = []

#iterate over initial list
#multiply each number by itself
#use .append[] method, to add the square to the new list, square_numbers

for num in numbers: 
    square_numbers.append[num * num]

#print new list
print[square_numbers]

#output
#[1, 4, 9, 16, 25, 36]
1 để tạo danh sách trong Python

Một cách để tạo danh sách trong Python là sử dụng vòng lặp

#initial list of numbers
numbers = [1,2,3,4,5,6]

#create a new,empty list to hold their squares
square_numbers = []

#iterate over initial list
#multiply each number by itself
#use .append[] method, to add the square to the new list, square_numbers

for num in numbers: 
    square_numbers.append[num * num]

#print new list
print[square_numbers]

#output
#[1, 4, 9, 16, 25, 36]
1

Ví dụ: bạn có thể sử dụng hàm

#initial list of numbers
numbers = [1,2,3,4,5,6]

#create a new,empty list to hold their squares
square_numbers = []

#iterate over initial list
#multiply each number by itself
#use .append[] method, to add the square to the new list, square_numbers

for num in numbers: 
    square_numbers.append[num * num]

#print new list
print[square_numbers]

#output
#[1, 4, 9, 16, 25, 36]
3 để tạo danh sách các số từ 0 - 4

#first create an empty list
my_list = []

#iterate over the numbers 0 - 4 using the range[] function
#range[5] creates an iterable, starting from 0 up to [but not including] 5
#Use the .append[] method to add the numbers 0 - 4 to my_list

for num in range[5]:
    my_list.append[num]
    
#print my_list
print[my_list]

#output
#[0, 1, 2, 3, 4]

Điều gì sẽ xảy ra nếu bạn đã có một danh sách các số nhưng muốn tạo một danh sách mới với các ô vuông của chúng?

Bạn lại có thể sử dụng vòng lặp

#initial list of numbers
numbers = [1,2,3,4,5,6]

#create a new,empty list to hold their squares
square_numbers = []

#iterate over initial list
#multiply each number by itself
#use .append[] method, to add the square to the new list, square_numbers

for num in numbers: 
    square_numbers.append[num * num]

#print new list
print[square_numbers]

#output
#[1, 4, 9, 16, 25, 36]
1, như vậy

#initial list of numbers
numbers = [1,2,3,4,5,6]

#create a new,empty list to hold their squares
square_numbers = []

#iterate over initial list
#multiply each number by itself
#use .append[] method, to add the square to the new list, square_numbers

for num in numbers: 
    square_numbers.append[num * num]

#print new list
print[square_numbers]

#output
#[1, 4, 9, 16, 25, 36]

Nhưng có một cách nhanh hơn và ngắn gọn hơn để đạt được kết quả tương tự – bằng cách sử dụng khả năng hiểu danh sách

Hiểu danh sách trong Python là gì?

Khi bạn đang phân tích và làm việc với các danh sách trong Python, bạn sẽ thường xuyên phải thao tác, sửa đổi hoặc thực hiện các phép tính trên từng mục trong danh sách, tất cả cùng một lúc

Bạn cũng có thể cần tạo danh sách mới từ đầu hoặc tạo danh sách mới dựa trên các giá trị của danh sách đã tồn tại

Hiểu danh sách là một cách nhanh chóng, ngắn gọn và thanh lịch để tạo danh sách so với các phương pháp lặp khác, như vòng lặp

#initial list of numbers
numbers = [1,2,3,4,5,6]

#create a new,empty list to hold their squares
square_numbers = []

#iterate over initial list
#multiply each number by itself
#use .append[] method, to add the square to the new list, square_numbers

for num in numbers: 
    square_numbers.append[num * num]

#print new list
print[square_numbers]

#output
#[1, 4, 9, 16, 25, 36]
1

Cú pháp chung để hiểu danh sách trông như thế này

new_list = [expression for variable in iterable]

Hãy phá vỡ nó

  • Việc hiểu danh sách bắt đầu và kết thúc bằng cách mở và đóng dấu ngoặc vuông,
    #initial list of numbers
    numbers = [1,2,3,4,5,6]
    
    #create a new,empty list to hold their squares
    square_numbers = []
    
    #iterate over initial list
    #multiply each number by itself
    #use .append[] method, to add the square to the new list, square_numbers
    
    for num in numbers: 
        square_numbers.append[num * num]
    
    #print new list
    print[square_numbers]
    
    #output
    #[1, 4, 9, 16, 25, 36]
    
    6
  • Sau đó, đến
    #initial list of numbers
    numbers = [1,2,3,4,5,6]
    
    #create a new,empty list to hold their squares
    square_numbers = []
    
    #iterate over initial list
    #multiply each number by itself
    #use .append[] method, to add the square to the new list, square_numbers
    
    for num in numbers: 
        square_numbers.append[num * num]
    
    #print new list
    print[square_numbers]
    
    #output
    #[1, 4, 9, 16, 25, 36]
    
    7 hoặc thao tác bạn muốn thực hiện và thực hiện trên từng giá trị bên trong lần lặp hiện tại. Kết quả của những tính toán này nhập vào danh sách mới
  • Các
    #initial list of numbers
    numbers = [1,2,3,4,5,6]
    
    #create a new,empty list to hold their squares
    square_numbers = []
    
    #iterate over initial list
    #multiply each number by itself
    #use .append[] method, to add the square to the new list, square_numbers
    
    for num in numbers: 
        square_numbers.append[num * num]
    
    #print new list
    print[square_numbers]
    
    #output
    #[1, 4, 9, 16, 25, 36]
    
    7 được theo sau bởi một mệnh đề
    #initial list of numbers
    numbers = [1,2,3,4,5,6]
    
    #create a new,empty list to hold their squares
    square_numbers = []
    
    #iterate over initial list
    #multiply each number by itself
    #use .append[] method, to add the square to the new list, square_numbers
    
    for num in numbers: 
        square_numbers.append[num * num]
    
    #print new list
    print[square_numbers]
    
    #output
    #[1, 4, 9, 16, 25, 36]
    
    1
  • new_list = [expression for variable in iterable]
    
    0 là tên tạm thời bạn muốn sử dụng cho từng mục trong danh sách hiện tại đang trải qua quá trình lặp lại
  • Từ khóa
    new_list = [expression for variable in iterable]
    
    1 được sử dụng để lặp qua iterable
  • new_list = [expression for variable in iterable]
    
    2 có thể là bất kỳ đối tượng Python nào, chẳng hạn như danh sách, bộ dữ liệu, chuỗi, v.v.
  • Từ phép lặp đã được thực hiện và các tính toán diễn ra trên từng mục trong quá trình lặp, các giá trị mới được tạo ra và được lưu vào một biến, trong trường hợp này là
    new_list = [expression for variable in iterable]
    
    3. Danh sách cũ [hoặc đối tượng khác] sẽ không thay đổi
  • Có thể có một câu lệnh
    new_list = [expression for variable in iterable]
    
    4 tùy chọn và mệnh đề bổ sung
    #initial list of numbers
    numbers = [1,2,3,4,5,6]
    
    #create a new,empty list to hold their squares
    square_numbers = []
    
    #iterate over initial list
    #multiply each number by itself
    #use .append[] method, to add the square to the new list, square_numbers
    
    for num in numbers: 
        square_numbers.append[num * num]
    
    #print new list
    print[square_numbers]
    
    #output
    #[1, 4, 9, 16, 25, 36]
    
    1

Cách sử dụng khả năng hiểu danh sách trong Python

Sử dụng cùng một ví dụ từ trước đó, đây là cách bạn tạo một danh sách mới các số từ 0 - 4 với hàm

#initial list of numbers
numbers = [1,2,3,4,5,6]

#create a new,empty list to hold their squares
square_numbers = []

#iterate over initial list
#multiply each number by itself
#use .append[] method, to add the square to the new list, square_numbers

for num in numbers: 
    square_numbers.append[num * num]

#print new list
print[square_numbers]

#output
#[1, 4, 9, 16, 25, 36]
3 chỉ trong một dòng, sử dụng khả năng hiểu danh sách

#initial list of numbers
numbers = [1,2,3,4,5,6]

#create a new,empty list to hold their squares
square_numbers = []

#iterate over initial list
#multiply each number by itself
#use .append[] method, to add the square to the new list, square_numbers

for num in numbers: 
    square_numbers.append[num * num]

#print new list
print[square_numbers]

#output
#[1, 4, 9, 16, 25, 36]
9

Điều này có cùng kết quả như ví dụ về vòng lặp

#initial list of numbers
numbers = [1,2,3,4,5,6]

#create a new,empty list to hold their squares
square_numbers = []

#iterate over initial list
#multiply each number by itself
#use .append[] method, to add the square to the new list, square_numbers

for num in numbers: 
    square_numbers.append[num * num]

#print new list
print[square_numbers]

#output
#[1, 4, 9, 16, 25, 36]
1, nhưng với ít mã hơn đáng kể

Hãy phá vỡ nó

  • iterable trong trường hợp này là một dãy số từ 0 đến 4, sử dụng
    new_list = [expression for variable in iterable]
    
    8.
    #initial list of numbers
    numbers = [1,2,3,4,5,6]
    
    #create a new,empty list to hold their squares
    square_numbers = []
    
    #iterate over initial list
    #multiply each number by itself
    #use .append[] method, to add the square to the new list, square_numbers
    
    for num in numbers: 
        square_numbers.append[num * num]
    
    #print new list
    print[square_numbers]
    
    #output
    #[1, 4, 9, 16, 25, 36]
    
    3 xây dựng một danh sách các số
  • Bạn sử dụng từ khóa
    new_list = [expression for variable in iterable]
    
    1 để lặp lại các số
  • #initial list of numbers
    numbers = [1,2,3,4,5,6]
    
    #create a new,empty list to hold their squares
    square_numbers = []
    
    #iterate over initial list
    #multiply each number by itself
    #use .append[] method, to add the square to the new list, square_numbers
    
    for num in numbers: 
        square_numbers.append[num * num]
    
    #print new list
    print[square_numbers]
    
    #output
    #[1, 4, 9, 16, 25, 36]
    
    91 theo sau mệnh đề
    #initial list of numbers
    numbers = [1,2,3,4,5,6]
    
    #create a new,empty list to hold their squares
    square_numbers = []
    
    #iterate over initial list
    #multiply each number by itself
    #use .append[] method, to add the square to the new list, square_numbers
    
    for num in numbers: 
        square_numbers.append[num * num]
    
    #print new list
    print[square_numbers]
    
    #output
    #[1, 4, 9, 16, 25, 36]
    
    1 là một biến, tên tạm thời cho mỗi giá trị trong iterable. Vì vậy,
    #initial list of numbers
    numbers = [1,2,3,4,5,6]
    
    #create a new,empty list to hold their squares
    square_numbers = []
    
    #iterate over initial list
    #multiply each number by itself
    #use .append[] method, to add the square to the new list, square_numbers
    
    for num in numbers: 
        square_numbers.append[num * num]
    
    #print new list
    print[square_numbers]
    
    #output
    #[1, 4, 9, 16, 25, 36]
    
    91 sẽ bằng với
    #initial list of numbers
    numbers = [1,2,3,4,5,6]
    
    #create a new,empty list to hold their squares
    square_numbers = []
    
    #iterate over initial list
    #multiply each number by itself
    #use .append[] method, to add the square to the new list, square_numbers
    
    for num in numbers: 
        square_numbers.append[num * num]
    
    #print new list
    print[square_numbers]
    
    #output
    #[1, 4, 9, 16, 25, 36]
    
    94 trong lần lặp đầu tiên, sau đó
    #initial list of numbers
    numbers = [1,2,3,4,5,6]
    
    #create a new,empty list to hold their squares
    square_numbers = []
    
    #iterate over initial list
    #multiply each number by itself
    #use .append[] method, to add the square to the new list, square_numbers
    
    for num in numbers: 
        square_numbers.append[num * num]
    
    #print new list
    print[square_numbers]
    
    #output
    #[1, 4, 9, 16, 25, 36]
    
    91 sẽ bằng với
    #initial list of numbers
    numbers = [1,2,3,4,5,6]
    
    #create a new,empty list to hold their squares
    square_numbers = []
    
    #iterate over initial list
    #multiply each number by itself
    #use .append[] method, to add the square to the new list, square_numbers
    
    for num in numbers: 
        square_numbers.append[num * num]
    
    #print new list
    print[square_numbers]
    
    #output
    #[1, 4, 9, 16, 25, 36]
    
    96 trong lần lặp tiếp theo, v.v., cho đến khi nó đạt và bằng số 4, tại đó quá trình lặp sẽ dừng lại
  • #initial list of numbers
    numbers = [1,2,3,4,5,6]
    
    #create a new,empty list to hold their squares
    square_numbers = []
    
    #iterate over initial list
    #multiply each number by itself
    #use .append[] method, to add the square to the new list, square_numbers
    
    for num in numbers: 
        square_numbers.append[num * num]
    
    #print new list
    print[square_numbers]
    
    #output
    #[1, 4, 9, 16, 25, 36]
    
    91 trước mệnh đề
    #initial list of numbers
    numbers = [1,2,3,4,5,6]
    
    #create a new,empty list to hold their squares
    square_numbers = []
    
    #iterate over initial list
    #multiply each number by itself
    #use .append[] method, to add the square to the new list, square_numbers
    
    for num in numbers: 
        square_numbers.append[num * num]
    
    #print new list
    print[square_numbers]
    
    #output
    #[1, 4, 9, 16, 25, 36]
    
    1 là một biểu thức cho từng mục trong chuỗi
  • Cuối cùng, danh sách mới [hoặc danh sách có thể lặp lại khác] được tạo sẽ được lưu trữ trong biến
    new_list = [expression for variable in iterable]
    
    3

Bạn thậm chí có thể thực hiện các phép toán trên các mục có trong iterable và kết quả sẽ được thêm vào danh sách mới

#first create an empty list
my_list = []

#iterate over the numbers 0 - 4 using the range[] function
#range[5] creates an iterable, starting from 0 up to [but not including] 5
#Use the .append[] method to add the numbers 0 - 4 to my_list

for num in range[5]:
    my_list.append[num]
    
#print my_list
print[my_list]

#output
#[0, 1, 2, 3, 4]
3

Ở đây, mỗi số trong

new_list = [expression for variable in iterable]
8 sẽ được nhân với hai và giá trị mới sẽ được lưu trữ trong biến
new_list = [expression for variable in iterable]
3

Điều gì sẽ xảy ra nếu bạn có sẵn một danh sách mà bạn muốn thao tác và sửa đổi từng mục trong đó?

Một lần nữa, bạn có thể đạt được điều đó chỉ với một dòng mã, sử dụng khả năng hiểu danh sách

#first create an empty list
my_list = []

#iterate over the numbers 0 - 4 using the range[] function
#range[5] creates an iterable, starting from 0 up to [but not including] 5
#Use the .append[] method to add the numbers 0 - 4 to my_list

for num in range[5]:
    my_list.append[num]
    
#print my_list
print[my_list]

#output
#[0, 1, 2, 3, 4]
6

Cách sử dụng điều kiện với khả năng hiểu danh sách trong Python

Theo tùy chọn, bạn có thể sử dụng câu lệnh

new_list = [expression for variable in iterable]
4 với khả năng hiểu danh sách

Cú pháp chung trông như thế này

#first create an empty list
my_list = []

#iterate over the numbers 0 - 4 using the range[] function
#range[5] creates an iterable, starting from 0 up to [but not including] 5
#Use the .append[] method to add the numbers 0 - 4 to my_list

for num in range[5]:
    my_list.append[num]
    
#print my_list
print[my_list]

#output
#[0, 1, 2, 3, 4]
8

Điều kiện hoạt động như một bộ lọc và thêm kiểm tra bổ sung để có thêm độ chính xác và tùy chỉnh khi tạo danh sách mới

Điều này có nghĩa là giá trị trong biểu thức phải đáp ứng một số tiêu chí nhất định và một điều kiện nhất định mà bạn chỉ định thì mới được đưa vào danh sách mới

#first create an empty list
my_list = []

#iterate over the numbers 0 - 4 using the range[] function
#range[5] creates an iterable, starting from 0 up to [but not including] 5
#Use the .append[] method to add the numbers 0 - 4 to my_list

for num in range[5]:
    my_list.append[num]
    
#print my_list
print[my_list]

#output
#[0, 1, 2, 3, 4]
9

Trong ví dụ trên, chỉ những giá trị mà điều kiện

#first create an empty list
my_list = []

#iterate over the numbers 0 - 4 using the range[] function
#range[5] creates an iterable, starting from 0 up to [but not including] 5
#Use the .append[] method to add the numbers 0 - 4 to my_list

for num in range[5]:
    my_list.append[num]
    
#print my_list
print[my_list]

#output
#[0, 1, 2, 3, 4]
33 được kiểm tra và đánh giá là True mới được nhập vào
new_list = [expression for variable in iterable]
3

Toán tử modulo được sử dụng trên mỗi một số trong dãy số bắt đầu từ 0 và kết thúc bằng 49

Nếu số dư của các số khi chia cho 2 bằng 0 thì và chỉ khi đó nó mới vào danh sách

Vì vậy, trong trường hợp này, nó tạo ra một danh sách chỉ các số chẵn

Sau đó, bạn có thể làm cho nó cụ thể như bạn muốn

Ví dụ: bạn có thể thêm nhiều điều kiện, như vậy

#initial list of numbers
numbers = [1,2,3,4,5,6]

#create a new,empty list to hold their squares
square_numbers = []

#iterate over initial list
#multiply each number by itself
#use .append[] method, to add the square to the new list, square_numbers

for num in numbers: 
    square_numbers.append[num * num]

#print new list
print[square_numbers]

#output
#[1, 4, 9, 16, 25, 36]
2

Trong ví dụ này, có hai điều kiện

#first create an empty list
my_list = []

#iterate over the numbers 0 - 4 using the range[] function
#range[5] creates an iterable, starting from 0 up to [but not including] 5
#Use the .append[] method to add the numbers 0 - 4 to my_list

for num in range[5]:
    my_list.append[num]
    
#print my_list
print[my_list]

#output
#[0, 1, 2, 3, 4]
35 và
#first create an empty list
my_list = []

#iterate over the numbers 0 - 4 using the range[] function
#range[5] creates an iterable, starting from 0 up to [but not including] 5
#Use the .append[] method to add the numbers 0 - 4 to my_list

for num in range[5]:
    my_list.append[num]
    
#print my_list
print[my_list]

#output
#[0, 1, 2, 3, 4]
33

Toán tử

#first create an empty list
my_list = []

#iterate over the numbers 0 - 4 using the range[] function
#range[5] creates an iterable, starting from 0 up to [but not including] 5
#Use the .append[] method to add the numbers 0 - 4 to my_list

for num in range[5]:
    my_list.append[num]
    
#print my_list
print[my_list]

#output
#[0, 1, 2, 3, 4]
37 chỉ ra rằng cả hai phải được đáp ứng để giá trị được thêm vào danh sách mới

Các giá trị không đáp ứng các điều kiện bị loại trừ và không được thêm vào

Cách sử dụng khả năng hiểu danh sách trên các chuỗi trong Python

Bạn có thể tạo một danh sách mới với các ký tự riêng lẻ có trong một chuỗi nhất định

#initial list of numbers
numbers = [1,2,3,4,5,6]

#create a new,empty list to hold their squares
square_numbers = []

#iterate over initial list
#multiply each number by itself
#use .append[] method, to add the square to the new list, square_numbers

for num in numbers: 
    square_numbers.append[num * num]

#print new list
print[square_numbers]

#output
#[1, 4, 9, 16, 25, 36]
6

Danh sách mới được tạo bao gồm tất cả các chữ cái riêng biệt có trong chuỗi "Python", hoạt động như một lần lặp

Cũng giống như các số, bạn có thể thực hiện các thao tác trên các ký tự có trong một chuỗi và tùy chỉnh chúng tùy thuộc vào cách bạn muốn chúng xuất hiện trong danh sách mới mà bạn tạo

Nếu bạn muốn tất cả các chữ cái đều là chữ hoa, bạn sẽ làm như sau

#initial list of numbers
numbers = [1,2,3,4,5,6]

#create a new,empty list to hold their squares
square_numbers = []

#iterate over initial list
#multiply each number by itself
#use .append[] method, to add the square to the new list, square_numbers

for num in numbers: 
    square_numbers.append[num * num]

#print new list
print[square_numbers]

#output
#[1, 4, 9, 16, 25, 36]
0

Tại đây, bạn sử dụng phương thức

#first create an empty list
my_list = []

#iterate over the numbers 0 - 4 using the range[] function
#range[5] creates an iterable, starting from 0 up to [but not including] 5
#Use the .append[] method to add the numbers 0 - 4 to my_list

for num in range[5]:
    my_list.append[num]
    
#print my_list
print[my_list]

#output
#[0, 1, 2, 3, 4]
38 để chuyển đổi từng chữ cái trong "Python" thành chữ hoa và thêm chúng vào biến
#first create an empty list
my_list = []

#iterate over the numbers 0 - 4 using the range[] function
#range[5] creates an iterable, starting from 0 up to [but not including] 5
#Use the .append[] method to add the numbers 0 - 4 to my_list

for num in range[5]:
    my_list.append[num]
    
#print my_list
print[my_list]

#output
#[0, 1, 2, 3, 4]
39

Điều tương tự cũng xảy ra nếu bạn muốn tất cả các chữ cái của mình là chữ thường - thay vào đó, bạn nên sử dụng phương pháp

#first create an empty list
my_list = []

#iterate over the numbers 0 - 4 using the range[] function
#range[5] creates an iterable, starting from 0 up to [but not including] 5
#Use the .append[] method to add the numbers 0 - 4 to my_list

for num in range[5]:
    my_list.append[num]
    
#print my_list
print[my_list]

#output
#[0, 1, 2, 3, 4]
60

Phần kết luận

Và bạn có nó rồi đấy. Bây giờ bạn đã biết kiến ​​thức cơ bản về hiểu danh sách trong Python

Nó cung cấp một cú pháp thanh lịch và ngắn gọn để tạo danh sách mới dựa trên danh sách hiện có hoặc các lần lặp khác

Nếu bạn muốn tìm hiểu thêm về Python, freeCodeCamp có Chứng chỉ Python

Trong chương trình giảng dạy dựa trên dự án này, bạn sẽ bắt đầu học các nguyên tắc cơ bản về lập trình và chuyển sang các chủ đề phức tạp hơn như cấu trúc dữ liệu. Bạn cũng sẽ xây dựng năm dự án để thực hành những gì bạn đã học

Cảm ơn đã đọc và mã hóa hạnh phúc

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

Dionysia Lemonaki

Học một cái gì đó mới mỗi ngày và viết về nó

Nếu bài viết này hữu ích, hãy tweet nó

Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu

Làm thế nào để hiểu danh sách hoạt động trong Python?

Việc hiểu danh sách Python bao gồm các dấu ngoặc vuông chứa biểu thức, được thực thi cho từng phần tử cùng với vòng lặp for để lặp qua từng phần tử trong danh sách Python. Python List comprehension provides a much more short syntax for creating a new list based on the values of an existing list.

Danh sách hiểu được sử dụng ở đâu?

Tính năng hiểu danh sách được sử dụng khi bạn muốn tạo danh sách mới từ các giá trị đã có trong liên kết hiện có . Khả năng hiểu danh sách cung cấp cú pháp ngắn hơn. Đọc thêm. Trong hướng dẫn Hiểu danh sách Python này, chúng ta sẽ tìm hiểu danh sách Python là gì và cách sử dụng chúng.

danh sách là gì [

[-1] có nghĩa là phần tử cuối cùng trong một chuỗi , trong trường hợp này là danh sách các bộ như [phần tử, số .

Bạn có thể sử dụng khả năng hiểu danh sách trên từ điển Python không?

Hiểu danh sách là một cách thuận tiện và nhanh hơn để tạo danh sách trong Python chỉ bằng một dòng mã . Nó giúp chúng ta viết các vòng lặp for dễ đọc trong một dòng. Trong Python, từ điển là một cấu trúc dữ liệu để lưu trữ dữ liệu sao cho mỗi phần tử của dữ liệu được lưu trữ được liên kết với một khóa.

Chủ Đề