Cấu trúc dữ liệu cơ bản nhất trong Python là dãy. Mỗi phần tử của một chuỗi được gán một số - vị trí hoặc chỉ mục của nó. Chỉ số đầu tiên bằng 0, chỉ số thứ hai là một, v.v.
Python có sáu loại trình tự tích hợp sẵn, nhưng những loại phổ biến nhất là danh sách và bộ dữ liệu, mà chúng ta sẽ thấy trong hướng dẫn này
Có một số điều bạn có thể làm với tất cả các loại trình tự. Các hoạt động này bao gồm lập chỉ mục, cắt, thêm, nhân và kiểm tra tư cách thành viên. Ngoài ra, Python có các hàm tích hợp để tìm độ dài của một chuỗi và tìm các phần tử lớn nhất và nhỏ nhất của nó
Danh sách Python
Danh sách này là kiểu dữ liệu linh hoạt nhất có sẵn trong Python, có thể được viết dưới dạng danh sách các giá trị [mục] được phân tách bằng dấu phẩy giữa các dấu ngoặc vuông. Điều quan trọng về danh sách là các mục trong danh sách không nhất thiết phải cùng loại
Tạo danh sách đơn giản như đặt các giá trị được phân tách bằng dấu phẩy khác nhau giữa các dấu ngoặc vuông. Ví dụ -
list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"];
Tương tự như chỉ mục chuỗi, chỉ mục danh sách bắt đầu từ 0 và danh sách có thể được cắt, nối, v.v.
Truy cập các giá trị trong danh sách
Để truy cập các giá trị trong danh sách, hãy sử dụng dấu ngoặc vuông để cắt cùng với chỉ mục hoặc các chỉ mục để lấy giá trị có sẵn tại chỉ mục đó. Ví dụ -
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]
Khi đoạn mã trên được thực thi, nó tạo ra kết quả sau -
list1[0]: physics list2[1:5]: [2, 3, 4, 5]
Cập nhật danh sách
Bạn có thể cập nhật một hoặc nhiều phần tử của danh sách bằng cách đưa lát cắt ở phía bên trái của toán tử gán và bạn có thể thêm vào các phần tử trong danh sách bằng phương thức append[]. Ví dụ -
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]
Lưu ý - Phương thức append[] được thảo luận trong phần tiếp theo
Khi đoạn mã trên được thực thi, nó tạo ra kết quả sau -
Value available at index 2 : 1997 New value available at index 2 : 2001
Xóa phần tử danh sách
Để xóa một phần tử danh sách, bạn có thể sử dụng câu lệnh del nếu bạn biết chính xác [những] phần tử nào bạn đang xóa. Bạn có thể sử dụng phương thức remove[] nếu không biết chính xác mục nào cần xóa. Ví dụ -
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]
Khi đoạn mã trên được thực thi, nó tạo ra kết quả sau -
['physics', 'chemistry', 1997, 2000] After deleting value at index 2 : ['physics', 'chemistry', 2000]
Lưu ý - phương thức remove[] được thảo luận trong phần tiếp theo
Hoạt động danh sách cơ bản
Các danh sách phản hồi các toán tử + và * giống như các chuỗi;
Trên thực tế, các danh sách đáp ứng tất cả các thao tác trình tự chung mà chúng ta đã sử dụng trên các chuỗi trong chương trước
Danh sách Python giống như các mảng có kích thước động, được khai báo bằng các ngôn ngữ khác [vector trong C++ và ArrayList trong Java]. Nói một cách đơn giản, danh sách là một tập hợp các thứ, được đặt trong [ ] và được phân tách bằng dấu phẩy.
Danh sách là một kiểu dữ liệu trình tự được sử dụng để lưu trữ bộ sưu tập dữ liệu. Tuples và String là các kiểu dữ liệu chuỗi khác
Ví dụ về danh sách trong Python
Ở đây chúng tôi đang tạo Danh sách Python bằng []
Python3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']0______71
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2______73
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']5
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]70
đầu ra
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]8
Danh sách là các thùng chứa đơn giản nhất là một phần không thể thiếu của ngôn ngữ Python. Các danh sách không cần phải luôn đồng nhất, điều này khiến nó trở thành công cụ mạnh nhất trong Python. Một danh sách có thể chứa các Kiểu dữ liệu như Số nguyên, Chuỗi, cũng như Đối tượng. Danh sách có thể thay đổi và do đó, chúng có thể được thay đổi ngay cả sau khi tạo
Tạo một danh sách trong Python
Danh sách trong Python có thể được tạo bằng cách đặt chuỗi bên trong dấu ngoặc vuông[]. Không giống như Bộ, danh sách không cần chức năng tích hợp để tạo danh sách.
Ghi chú. Không giống như Bộ, danh sách có thể chứa các phần tử có thể thay đổi.
ví dụ 1. Tạo một danh sách trong Python
Python3
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]71
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]72
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]73
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]76
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]79
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
list1[0]: physics list2[1:5]: [2, 3, 4, 5]85
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2____289
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]91
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]93
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]97
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]13
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]14
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2______73
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
_______79____178____586
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']02
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']03
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']03
Đầu ra
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Sự phức tạp để tạo danh sách
Thời gian phức tạp. Ô[1]
Độ phức tạp không gian. Trên]
ví dụ 2. Tạo danh sách có nhiều phần tử riêng biệt hoặc trùng lặp
Một danh sách có thể chứa các giá trị trùng lặp với các vị trí riêng biệt của chúng và do đó, nhiều giá trị khác biệt hoặc trùng lặp có thể được chuyển thành một chuỗi tại thời điểm tạo danh sách
Python3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']90
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']91
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']92
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2_______796
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']800
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']800
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']804
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']804
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']810
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']812
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']816
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']90
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']823
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']824
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2_______796
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']832
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']800
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']836
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']810
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']832
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']844
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
Đầu ra
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']
Truy cập các phần tử từ Danh sách
Để truy cập các mục danh sách, hãy tham khảo số chỉ mục. Sử dụng toán tử chỉ mục [ ] để truy cập một mục trong danh sách. Chỉ số phải là một số nguyên. Danh sách lồng nhau được truy cập bằng cách sử dụng chỉ mục lồng nhau.
ví dụ 1. Truy cập các phần tử từ danh sách
Python3
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]71
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']851
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']90
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']853
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2______73
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']863
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']864
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']867
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']02
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']03
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']03
Đầu ra
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]7
ví dụ 2. Truy cập các phần tử từ danh sách đa chiều
Python3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']881
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']882
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']885
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']832
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']836______7889
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']832
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']891
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']892
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']893
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']894
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']897
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']02
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']04
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']96
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']03
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']96
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']04
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']02
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']03
Đầu ra
list1[0]: physics list2[1:5]: [2, 3, 4, 5]8
lập chỉ mục tiêu cực
Trong Python, các chỉ mục chuỗi âm biểu thị các vị trí từ cuối mảng. Thay vì phải tính offset như trong List[len[List]-3], chỉ cần viết List[-3] là đủ. Lập chỉ mục phủ định có nghĩa là bắt đầu từ cuối, -1 đề cập đến mục cuối cùng, -2 đề cập đến mục cuối cùng thứ hai, v.v.
Python3
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2_______796
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']832
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']800
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']836
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']810
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']832
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']32
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']33
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']36
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']38
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']43
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']96
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']03
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']46
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']43
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']804
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']03
Đầu ra
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]9
Sự phức tạp khi truy cập các phần tử trong Danh sách
Thời gian phức tạp. Ô[1]
Độ phức tạp không gian. Ô[1]
Lấy kích thước của danh sách Python
Python len[] được sử dụng để lấy độ dài của danh sách
Python3
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]73
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']55
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]76
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']60
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']61
list1[0]: physics list2[1:5]: [2, 3, 4, 5]85
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']63
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2
list1[0]: physics list2[1:5]: [2, 3, 4, 5]89
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]91
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]93
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']60
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']75
Đầu ra
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]1
Lấy đầu vào của danh sách Python
Chúng ta có thể lấy đầu vào của một danh sách các phần tử là chuỗi, số nguyên, dấu phẩy, v.v. Nhưng cái mặc định là một chuỗi
ví dụ 1.
Python3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']76
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']77
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']79
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']81
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']83
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']85
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']87
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']88
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']90
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']93
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']94
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']95
đầu ra
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]8
ví dụ 2
con trăn
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']96
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']97
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']99
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']83
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]703
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]704
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]705
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]706
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']88
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]709
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]711
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']99
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']83
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]716
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]717
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']95
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']93
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']94
đầu ra
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']0
Để biết thêm xem cái này
Thêm phần tử vào danh sách Python
Phương pháp 1. Sử dụng phương thức append[]
Các phần tử có thể được thêm vào Danh sách bằng cách sử dụng hàm append[] tích hợp sẵn. Mỗi lần chỉ có thể thêm một phần tử vào danh sách bằng cách sử dụng phương thức append[], để thêm nhiều phần tử bằng phương thức append[], các vòng lặp được sử dụng. Các bộ dữ liệu cũng có thể được thêm vào danh sách bằng cách sử dụng phương thức append vì các bộ dữ liệu là bất biến. Không giống như Bộ, Danh sách cũng có thể được thêm vào danh sách hiện có bằng cách sử dụng phương thức append[]
Python3
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]71
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]724
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]73
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]76
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]731
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]737
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]738
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]740
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']96
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]740
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]740
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']800
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]753
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]759
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]760
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]761
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]762
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]763
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]764
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']96
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']800
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]769
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]770
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]772
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]775
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]781
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]783
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']812
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']810
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]704
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]790
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]796
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']63
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']836
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']832
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]805
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
list1[0]: physics list2[1:5]: [2, 3, 4, 5]808
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
Đầu ra
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
Sự phức tạp khi thêm các phần tử trong một Lists[append[] method]
Thời gian phức tạp. Ô[1]
Độ phức tạp không gian. Ô[1]
Phương pháp 2. Sử dụng phương thức chèn[]
Phương thức append[] chỉ có tác dụng khi thêm phần tử vào cuối List, để thêm phần tử vào vị trí mong muốn thì sử dụng phương thức insert[]. Không giống như append[] chỉ nhận một đối số, phương thức insert[] yêu cầu hai đối số [vị trí, giá trị].
Python3
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]71
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]724
list1[0]: physics list2[1:5]: [2, 3, 4, 5]816
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]73
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2_______796
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']804
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']800
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
list1[0]: physics list2[1:5]: [2, 3, 4, 5]831
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
list1[0]: physics list2[1:5]: [2, 3, 4, 5]837
list1[0]: physics list2[1:5]: [2, 3, 4, 5]838
list1[0]: physics list2[1:5]: [2, 3, 4, 5]839
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]841
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']804
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
list1[0]: physics list2[1:5]: [2, 3, 4, 5]844
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]841
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']02
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']832
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
list1[0]: physics list2[1:5]: [2, 3, 4, 5]854
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
Đầu ra
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']80
Sự phức tạp khi thêm các phần tử trong một Lists[insert[] method]
Thời gian phức tạp. Trên]
Độ phức tạp không gian. Ô[1]
Phương pháp 3. Sử dụng phương thức mở rộng[]
Ngoài các phương thức append[] và insert[], còn một phương thức nữa để cộng phần tử là Extend[], phương thức này dùng để thêm nhiều phần tử cùng lúc vào cuối danh sách
Ghi chú. Các phương thức append[] và expand[] chỉ có thể thêm các phần tử vào cuối
Python3
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]71
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]724
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]73
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2_______796
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']804
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']800
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
list1[0]: physics list2[1:5]: [2, 3, 4, 5]831
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
list1[0]: physics list2[1:5]: [2, 3, 4, 5]882
list1[0]: physics list2[1:5]: [2, 3, 4, 5]883
list1[0]: physics list2[1:5]: [2, 3, 4, 5]884
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]886
list1[0]: physics list2[1:5]: [2, 3, 4, 5]887
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']832
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
list1[0]: physics list2[1:5]: [2, 3, 4, 5]891
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']03
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
list1[0]: physics list2[1:5]: [2, 3, 4, 5]895
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
Đầu ra
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']81
Sự phức tạp khi thêm các phần tử trong một Lists[extend[] method]
Thời gian phức tạp. Trên]
Độ phức tạp không gian. Ô[1]
Đảo ngược danh sách
Một danh sách có thể được đảo ngược bằng cách sử dụng phương thức reverse[] trong Python
Python3
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]901
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]902
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']96
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']804
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']800
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']812
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]915
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]917
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]919
_______79____3921
Đầu ra
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']82
Xóa phần tử khỏi danh sách
Phương pháp 1. Sử dụng phương thức remove[]
Có thể xóa các phần tử khỏi Danh sách bằng cách sử dụng hàm remove[] tích hợp nhưng Lỗi sẽ phát sinh nếu phần tử không tồn tại trong danh sách. Phương thức Remove[] chỉ xóa một phần tử tại một thời điểm, để xóa một loạt phần tử, trình vòng lặp được sử dụng. Phương thức remove[] xóa mục đã chỉ định
Ghi chú. Phương thức xóa trong Danh sách sẽ chỉ xóa lần xuất hiện đầu tiên của phần tử được tìm kiếm
ví dụ 1
Python3
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]71
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]923
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]73
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2_______796
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']804
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']800
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']812
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']810
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]940
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]941
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
list1[0]: physics list2[1:5]: [2, 3, 4, 5]887
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]945
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
list1[0]: physics list2[1:5]: [2, 3, 4, 5]89
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]949
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
list1[0]: physics list2[1:5]: [2, 3, 4, 5]844
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
list1[0]: physics list2[1:5]: [2, 3, 4, 5]831
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]961
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]962
_______174____3964____7812
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
_______174____3964____7810
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]973
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
Đầu ra
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']83
ví dụ 2
Python3
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]73
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2_______796
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']804
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']800
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']812
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']810
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]940
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]941
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
list1[0]: physics list2[1:5]: [2, 3, 4, 5]887
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]945
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
list1[0]: physics list2[1:5]: [2, 3, 4, 5]89
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]949
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
list1[0]: physics list2[1:5]: [2, 3, 4, 5]844
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]961
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]109
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]761
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]762
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]763
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]764
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']96
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']812
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]769
_______1770____174____5121
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78____5124
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
Đầu ra
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']84
Sự phức tạp khi xóa các phần tử trong một Lists[phương thức remove[]]
Thời gian phức tạp. Trên]
Độ phức tạp không gian. Ô[1]
Phương pháp 2. Sử dụng phương thức pop[]
hàm pop[] cũng có thể được sử dụng để xóa và trả về một phần tử khỏi danh sách, nhưng theo mặc định, hàm này chỉ xóa phần tử cuối cùng của danh sách, để xóa một phần tử khỏi một vị trí cụ thể của Danh sách, chỉ số của phần tử được truyền
Python3
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2_______796
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']804
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']800
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']812
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]143
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]144
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74____5146
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78____5149
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]155
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]156
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]144
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]159
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78____5164
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
Đầu ra
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']85
Sự phức tạp khi xóa các phần tử trong phương thức Lists[pop[]]
Thời gian phức tạp. O[1]/O[n] [O[1] để xóa phần tử cuối cùng, O[n] để xóa phần tử đầu tiên và ở giữa]
Độ phức tạp không gian. Ô[1]
Cắt một danh sách
Chúng ta có thể lấy các chuỗi con và danh sách con bằng cách sử dụng một lát cắt. Trong Danh sách Python, có nhiều cách để in toàn bộ danh sách với tất cả các phần tử, nhưng để in một dải phần tử cụ thể từ danh sách, chúng ta sử dụng thao tác Slice.
Thao tác lát cắt được thực hiện trên Danh sách bằng cách sử dụng dấu hai chấm [. ].
Để in các phần tử từ đầu đến một phạm vi, hãy sử dụng
[. Mục lục]
Để in các phần tử từ mục đích sử dụng cuối
[. -Mục lục]
Để in các phần tử từ một Chỉ mục cụ thể cho đến khi sử dụng hết
[Mục lục. ]
Để in toàn bộ danh sách theo thứ tự ngược lại, hãy sử dụng
[. -1]
Lưu ý – Để in các thành phần của Danh sách từ phía sau, hãy sử dụng Chỉ mục phủ định.
TÌM HIỂU CẮT DANH SÁCH
- pr[0] truy cập mục đầu tiên, 2
- pr[-4] truy cập mục thứ tư từ cuối, 5
- pr[2. ] truy cập [5, 7, 11, 13], danh sách các mục từ thứ ba đến cuối cùng
- trước [. 4] truy cập [2, 3, 5, 7], danh sách các mục từ thứ nhất đến thứ tư
- pr[2. 4] truy cập [5, 7], danh sách các mục từ thứ ba đến thứ năm
- pr[1. 2] truy cập [3, 7, 13], các mục thay thế, bắt đầu từ mục thứ hai
Python3
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]71
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]923
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]73
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2_______5176
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]178
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]178
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]182
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]184
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]186
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]940
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]189
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]191
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]176
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]178
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]178
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]182
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]184
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
list1[0]: physics list2[1:5]: [2, 3, 4, 5]831
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]811
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]812
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]813
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']804
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]818
list1[0]: physics list2[1:5]: [2, 3, 4, 5]887
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78____5823
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
_______79____5826
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]827
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]828
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]813
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']812
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]834
_______79____178____5837
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]838
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]839
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
_______79____5826
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]843
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]844
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]813
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]848
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78____5851
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
_______79____5826
Đầu ra
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']86
Chỉ số phủ định Cắt lát danh sách
Python3
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]73
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2_______5176
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]178
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]178
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]182
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]184
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]186
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]940
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]189
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]191
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]176
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]178
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]178
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]182
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]184
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
list1[0]: physics list2[1:5]: [2, 3, 4, 5]831
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]894
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]895
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]813
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74____5899
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']43
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']810
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']005
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
_______79____5826
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]811
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']010
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]813
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']43
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']810
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]818
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']43
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']96
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']023
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
_______79____5826
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']027
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]812
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]813
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']032
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']43
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']96
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']038
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
_______79____5826
Đầu ra
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']87
Danh sách hiểu
Khả năng hiểu danh sách Python được sử dụng để tạo danh sách mới từ các lần lặp khác như bộ dữ liệu, chuỗi, mảng, danh sách, v.v. Việc hiểu danh sách bao gồm các dấu ngoặc 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ử.
cú pháp
danh sách mới = [ biểu thức [phần tử] cho phần tử trong danh sách cũ nếu điều kiện ]
Ví dụ.
Python3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']042
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']043
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']044
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']045
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']046
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']048
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']049
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']049
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]761
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']053
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]763
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]764
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']96
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]949
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']061
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']053
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']063
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']96
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']070
Đầu ra
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']88
Để hiểu rõ hơn, đoạn mã trên tương tự như sau.
Python3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']071
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']046
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]76
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]761
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']053
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]763
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]764
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]78
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']96
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']4
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]949
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]769
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ["list1[0]: ", list1[0]] print ["list2[1:5]: ", list2[1:5]]770
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']061
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']053
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']063
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']96
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print [list] del list[2] print ["After deleting value at index 2 : ", list]818
#!/usr/bin/python3 list = ['physics', 'chemistry', 1997, 2000] print ["Value available at index 2 : ", list[2]] list[2] = 2001 print ["New value available at index 2 : ", list[2]]940
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']094
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']049
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']049
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
list1[0]: physics list2[1:5]: [2, 3, 4, 5]80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']9
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']070
Đầu ra
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']88
Tham khảo các bài viết dưới đây để biết thông tin chi tiết về List Comprehension
- Hiểu và cắt danh sách Python
- Hiểu danh sách lồng nhau trong Python
- Hiểu danh sách và ord[] trong Python
Ví dụ cơ bản về Danh sách Python
- Chương trình Python để trao đổi các phần tử đầu tiên và cuối cùng trong một danh sách
- Chương trình Python hoán đổi hai phần tử trong danh sách
- Python – Hoán đổi các phần tử trong danh sách String
- con trăn. Các cách để tìm độ dài của danh sách
- Tối đa hai số trong Python
- Tối thiểu hai số trong Python
Để thực hành thao tác danh sách cơ bản, vui lòng đọc bài viết này – Danh sách chương trình Python
Liệt kê các phương thức
FunctionDescriptionAppend[]Thêm một phần tử vào cuối danh sáchExtend[]Thêm tất cả các phần tử của một danh sách vào một danh sách khácInsert[]Chèn một mục vào chỉ mục đã xác địnhRemove[]Xóa một mục khỏi danh sáchClear[]Xóa tất cả các mục khỏi listIndex[]Để biết thêm, hãy tham khảo bài viết này – Python List methods
Các hoạt động được đề cập ở trên tự sửa đổi danh sách
Các chức năng tích hợp với Danh sách
FunctionDescriptionreduce[] áp dụng một hàm cụ thể được truyền trong đối số của nó cho tất cả các thành phần danh sách lưu trữ kết quả trung gian và chỉ trả về giá trị tổng cuối cùngsum[]Tổng các số trong listord[]Trả về một số nguyên biểu thị điểm mã Unicode của giá trị đã cho . nếu danh sách trống, trả về falselen[]Trả về độ dài của danh sách hoặc kích thước của đối tượng lắng nghe[]Trả về đối tượng liệt kê của listaccumulate[]áp dụng một hàm cụ thể được truyền trong đối số của nó cho tất cả các phần tử danh sách trả về một danh sách chứa phần tử trung gianXem qua các bài báo gần đây về Danh sách
Liên kết hữu ích.
- Các bài báo gần đây về Danh sách Python
- Hướng dẫn Python
- Câu hỏi trắc nghiệm
- Tất cả các bài viết trong Danh mục Python
{ "@bối cảnh". “https. //lược đồ. tổ chức/”, “@type”. “Bài báo”, “mainEntityOfPage”. { "@loại". “Trang web”, “@id”. “https. //www. chuyên viên máy tính. org/python-list/” }, “tiêu đề”. “Danh sách Python”, “mô tả”. “Danh sách Python giống như các mảng có kích thước động, được khai báo bằng các ngôn ngữ khác [vector trong C++ và ArrayList trong Java]. ", "hình ảnh". “https. //phương tiện truyền thông. chuyên viên máy tính. org/wp-content/uploads/List-Slicing. jpg”, “tác giả”. { "@loại". "Tên tổ chức". “GeeksforGeeks” }, “nhà xuất bản”. { "@loại". "Tên tổ chức". “GeekforGeek”, “logo”. { "@loại". “ImageObject”, “url”. “https. //phương tiện truyền thông. chuyên viên máy tính. org/wp-content/cdn-uploads/gfg_200x200-min. png” } }, “datePublished”. “2022-07-18”, “dateModified”. “2022-07-21” }