Để gán bất kỳ giá trị nào cho một danh sách bằng cách sử dụng toán tử gán ở vị trí i
, a[i] = x
, kích thước của danh sách ít nhất phải là i+1
. Nếu không, nó sẽ tăng IndexError
, như hình bên dưới
1
2
3
4
5
6
7
8
if __name__ == '__main__'.
a = []
a[0] = 1
in[a]
# raise IndexError. liệt kê chỉ mục gán ngoài phạm vi
Tải xuống mã
Giải pháp là tạo một danh sách trống gồm None
khi không biết trước các mục trong danh sách. Điều này có thể dễ dàng thực hiện, như hình dưới đây.
1
2
3
4
5
6
7
if __name__ == '__main__'.
a = [Không có] * 5
in[a]
# bản in [Không, Không, Không, Không, Không]
Tải xuống Chạy mã
Mã trên sẽ tạo một danh sách có kích thước 5
, trong đó mỗi vị trí được khởi tạo bởi None
. None
thường được sử dụng trong Python để biểu thị sự vắng mặt của một giá trị.
Một cách khác để tạo danh sách trống với kích thước nhất định là sử dụng khả năng hiểu danh sách.
1
2
3
4
5
6
7
if __name__ == '__main__'.
a = [Không có for x in range[5]]
in[a]
# bản in [[], [], [], [], []]
Tải xuống Chạy mã
Sử dụng giải pháp nào?
Giải pháp đầu tiên hoạt động tốt đối với các loại không tham chiếu như số. Nhưng bạn có thể gặp phải lỗi tham chiếu trong một số trường hợp. Ví dụ:
demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
2 sẽ dẫn đến danh sách chứa cùng một đối tượng danh sách được lặp lại 5
lần Một danh sách có thể được xác định bằng các dấu ngoặc vuông bao quanh nó và các giá trị riêng lẻ được phân tách bằng dấu phẩy
Để lấy độ dài của danh sách trong Python, bạn có thể sử dụng hàm
demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
4 tích hợpNgoài hàm
demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
4, bạn cũng có thể sử dụng vòng lặp for và hàm demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
6 để lấy độ dài của danh sáchTrong bài viết này, tôi sẽ hướng dẫn các bạn cách lấy độ dài của một danh sách theo 3 cách khác nhau
Cách lấy độ dài của danh sách trong Python bằng vòng lặp For
Bạn có thể sử dụng vòng lặp for gốc của Python để lấy độ dài của danh sách vì giống như bộ dữ liệu và từ điển, danh sách có thể lặp lại
Phương pháp này thường được gọi là phương pháp ngây thơ
Ví dụ dưới đây cho bạn thấy cách sử dụng phương thức ngây thơ để lấy độ dài của danh sách trong Python
demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
# Initializing counter variable
counter = 0
for item in demoList:
# Incrementing counter variable to get each item in the list
counter = counter + 1
# Printing the result to the console by converting counter to string in order to get the number
print["The length of the list using the naive method is: " + str[counter]]
# Output: The length of the list using the naive method is: 7
Cách lấy độ dài của danh sách bằng hàm demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
4
demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
Sử dụng hàm
demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
4 là cách phổ biến nhất để lấy độ dài của một lần lặpĐiều này đơn giản hơn là sử dụng vòng lặp for
Cú pháp sử dụng phương thức
demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
4 là demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
0Đoạn mã dưới đây cho biết cách sử dụng hàm
demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
4 để lấy độ dài của danh sáchdemoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
Cách lấy độ dài của danh sách bằng hàm demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
6
demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
Phương thức
demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
6 là một cách ít được biết đến hơn để lấy độ dài của danh sách và các lần lặp khácdemoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
6 được xác định trong mô-đun toán tử, vì vậy bạn cần nhập nó từ đó trước khi có thể sử dụngCú pháp sử dụng phương thức
demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
6 là demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
5Ví dụ dưới đây cho bạn thấy cách sử dụng phương thức
demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
6 để lấy độ dài của danh sách từ toán tử nhập length_hintdemoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = length_hint[demoList]
print["The length of the list using the length_hint[] method is: " + str[sizeOfDemoList]]
# The length of the list using the length_hint[] method is: 7
Suy nghĩ cuối cùng
Bài viết này đã chỉ cho bạn cách lấy kích thước của danh sách bằng 3 phương pháp khác nhau. vòng lặp for, hàm
demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
4 và hàm demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
6 từ mô-đun toán tửBạn có thể tự hỏi nên sử dụng phương pháp nào giữa 3 phương pháp này
Tôi khuyên bạn nên sử dụng
demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
4 vì bạn không cần phải làm gì nhiều để sử dụng nó so với vòng lặp for và demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
6Ngoài ra,
demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
4 dường như nhanh hơn cả vòng lặp for và demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]
sizeOfDemoList = len[demoList]
print["The length of the list using the len[] method is: " + str[sizeOfDemoList]]
# Output: The length of the list using the len[] method is: 7
6Nếu bạn thấy bài viết này hữu ích, hãy chia sẻ nó để nó có thể đến được với những người cần nó
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
Nhà phát triển web và nhà văn kỹ thuật tập trung vào các công nghệ giao diện người dùng
Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơ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