Trong các kiểu dữ liệu chuỗi python, chúng ta có thể truy cập các phần tử bằng cách lập chỉ mục và cắt. Các kiểu dữ liệu chuỗi là các đối tượng chuỗi, danh sách, tuple, phạm vi. Hãy cùng tìm hiểu chi tiết về lập chỉ mục và cắt lát trong bài viết này
Các chủ đề được đề cập trong bài viết này
Image by AuthorLập chỉ mục và cắt lát là gì?
lập chỉ mục. Lập chỉ mục được sử dụng để có được các phần tử riêng lẻ
cắt lát. Cắt lát được sử dụng để có được một chuỗi các phần tử
Việc lập chỉ mục và cắt lát có thể được thực hiện trong các loại Trình tự Python như danh sách, chuỗi, tuple, đối tượng phạm vi
lập chỉ mụcLập chỉ mục bắt đầu từ
#Indexing - tuple
t=[1,2,3,4,5]
print [t[-6]]#Output:IndexError: tuple index out of range
#Indexing - range object
r=range[5]
print [r[-7]]#Output:IndexError: range object index out of range
#Indexing - List
n=[1,2,3,4,5]
print [n[6]]#Output:IndexError: list index out of range
#Indexing -string
s="python"
print [s[7]]#Output:IndexError: string index out of range
1. Chỉ số ____01 đại diện cho phần tử đầu tiên trong chuỗiLập chỉ mục tiêu cực bắt đầu từ -1. Chỉ số
#Indexing - tuple
t=[1,2,3,4,5]
print [t[-6]]#Output:IndexError: tuple index out of range
#Indexing - range object
r=range[5]
print [r[-7]]#Output:IndexError: range object index out of range
#Indexing - List
n=[1,2,3,4,5]
print [n[6]]#Output:IndexError: list index out of range
#Indexing -string
s="python"
print [s[7]]#Output:IndexError: string index out of range
3 đại diện cho phần tử cuối cùng trong chuỗiLập chỉ mục trong chuỗiLập chỉ mục theo chuỗi [Nguồn hình ảnh. Tác giả]
Ví dụ về lập chỉ mục trong chuỗi
s="Python"
print [s[0]]#Output:P
print [s[-1]]#Output:n
print [s[-2]]#Output:o
Lập chỉ mục trong danh sáchLập chỉ mục trong Danh sách [Nguồn hình ảnh. Tác giả]
Ví dụ về lập chỉ mục trong danh sách
list_=[10,20,30,40,50]
print [list_[0]]#Output:10
print [list_[-1]]#Output:50
print [list_[-2]]#Output:40
Lập chỉ mục trong đối tượng tuple và phạm vi____5Lỗi chỉ mụcCố gắng sử dụng một chỉ mục quá lớn sẽ dẫn đến IndexError
#Indexing - tuple
t=[1,2,3,4,5]
print [t[-6]]#Output:IndexError: tuple index out of range
#Indexing - range object
r=range[5]
print [r[-7]]#Output:IndexError: range object index out of range
#Indexing - List
n=[1,2,3,4,5]
print [n[6]]#Output:IndexError: list index out of range
#Indexing -string
s="python"
print [s[7]]#Output:IndexError: string index out of range
Chuyển nhượng để lập chỉ mục
đối tượng bất biến
Gán vào một vị trí được lập chỉ mục cho các đối tượng không thay đổi như chuỗi, bộ dữ liệu, đối tượng phạm vi sẽ tăng
#Indexing - tuple
t=[1,2,3,4,5]
print [t[-6]]#Output:IndexError: tuple index out of range
#Indexing - range object
r=range[5]
print [r[-7]]#Output:IndexError: range object index out of range
#Indexing - List
n=[1,2,3,4,5]
print [n[6]]#Output:IndexError: list index out of range
#Indexing -string
s="python"
print [s[7]]#Output:IndexError: string index out of range
4Gán mục trong bộ, chuỗi, đối tượng phạm vi
________số 8Nếu một bộ chứa các đối tượng có thể thay đổi như danh sách, chúng ta có thể thực hiện gán mục cho danh sách đó
t=[1,2,[3]]
#Item assignment supported for list element inside tuple.
t[2][0]=7
print [t]#Output:[1, 2, [7]]
đối tượng có thể thay đổi
Hỗ trợ gán mục trong Danh sách [đối tượng có thể thay đổi]
#Indexing - tuple
t=[1,2,3,4,5]
print [t[-6]]#Output:IndexError: tuple index out of range
#Indexing - range object
r=range[5]
print [r[-7]]#Output:IndexError: range object index out of range
#Indexing - List
n=[1,2,3,4,5]
print [n[6]]#Output:IndexError: list index out of range
#Indexing -string
s="python"
print [s[7]]#Output:IndexError: string index out of range
0cắt lát
- Cắt lát [Phạm vi chỉ mục]
Chúng tôi có thể chỉ định một loạt các chỉ số
#Indexing - tuple
t=[1,2,3,4,5]
print [t[-6]]#Output:IndexError: tuple index out of range
#Indexing - range object
r=range[5]
print [r[-7]]#Output:IndexError: range object index out of range
#Indexing - List
n=[1,2,3,4,5]
print [n[6]]#Output:IndexError: list index out of range
#Indexing -string
s="python"
print [s[7]]#Output:IndexError: string index out of range
1Cắt dâycắt lát cho phép bạn lấy chuỗi con
1.
#Indexing - tuple
t=[1,2,3,4,5]
print [t[-6]]#Output:IndexError: tuple index out of range
#Indexing - range object
r=range[5]
print [r[-7]]#Output:IndexError: range object index out of range
#Indexing - List
n=[1,2,3,4,5]
print [n[6]]#Output:IndexError: list index out of range
#Indexing -string
s="python"
print [s[7]]#Output:IndexError: string index out of range
5— Trả về phần tử từ chỉ mục đầu tiên đến chỉ mục thứ ba [không bao gồm]. #Indexing - tuple
t=[1,2,3,4,5]
print [t[-6]]#Output:IndexError: tuple index out of range
#Indexing - range object
r=range[5]
print [r[-7]]#Output:IndexError: range object index out of range
#Indexing - List
n=[1,2,3,4,5]
print [n[6]]#Output:IndexError: list index out of range
#Indexing -string
s="python"
print [s[7]]#Output:IndexError: string index out of range
32.
#Indexing - tuple
t=[1,2,3,4,5]
print [t[-6]]#Output:IndexError: tuple index out of range
#Indexing - range object
r=range[5]
print [r[-7]]#Output:IndexError: range object index out of range
#Indexing - List
n=[1,2,3,4,5]
print [n[6]]#Output:IndexError: list index out of range
#Indexing -string
s="python"
print [s[7]]#Output:IndexError: string index out of range
6-Trả về phần tử từ đầu chuỗi cho đến chỉ mục thứ ba [không bao gồm]#Indexing - tuple
t=[1,2,3,4,5]
print [t[-6]]#Output:IndexError: tuple index out of range
#Indexing - range object
r=range[5]
print [r[-7]]#Output:IndexError: range object index out of range
#Indexing - List
n=[1,2,3,4,5]
print [n[6]]#Output:IndexError: list index out of range
#Indexing -string
s="python"
print [s[7]]#Output:IndexError: string index out of range
53. Các chỉ số lát cắt có các giá trị mặc định hữu ích;
Chỉ mục bắt đầu bị bỏ qua
#Indexing - tuple
t=[1,2,3,4,5]
print [t[-6]]#Output:IndexError: tuple index out of range
#Indexing - range object
r=range[5]
print [r[-7]]#Output:IndexError: range object index out of range
#Indexing - List
n=[1,2,3,4,5]
print [n[6]]#Output:IndexError: list index out of range
#Indexing -string
s="python"
print [s[7]]#Output:IndexError: string index out of range
7-Trả về phần tử từ đầu chuỗi cho đến chỉ mục thứ balist_=[10,20,30,40,50]
print [list_[0]]#Output:10
print [list_[-1]]#Output:50
print [list_[-2]]#Output:40
0Bỏ qua chỉ số dừng
#Indexing - tuple
t=[1,2,3,4,5]
print [t[-6]]#Output:IndexError: tuple index out of range
#Indexing - range object
r=range[5]
print [r[-7]]#Output:IndexError: range object index out of range
#Indexing - List
n=[1,2,3,4,5]
print [n[6]]#Output:IndexError: list index out of range
#Indexing -string
s="python"
print [s[7]]#Output:IndexError: string index out of range
8-Trả về phần tử từ chỉ số thứ hai cho đến hết chuỗilist_=[10,20,30,40,50]
print [list_[0]]#Output:10
print [list_[-1]]#Output:50
print [list_[-2]]#Output:40
14. chỉ số tiêu cực
#Indexing - tuple
t=[1,2,3,4,5]
print [t[-6]]#Output:IndexError: tuple index out of range
#Indexing - range object
r=range[5]
print [r[-7]]#Output:IndexError: range object index out of range
#Indexing - List
n=[1,2,3,4,5]
print [n[6]]#Output:IndexError: list index out of range
#Indexing -string
s="python"
print [s[7]]#Output:IndexError: string index out of range
9-Trả về phần tử từ chỉ số cuối cùng thứ hai cho đến cuối chuỗilist_=[10,20,30,40,50]
print [list_[0]]#Output:10
print [list_[-1]]#Output:50
print [list_[-2]]#Output:40
25. Sử dụng chỉ mục bước
s="Python"
s[0]="j"
#Output:TypeError: 'str' object does not support item assignment
t=[1,2,3]
t[2]=7#Output:TypeError: 'tuple' object does not support item assignment
r=range[5]
r[0]=10
#Output:TypeError: 'range' object does not support item assignment
0-Trả về phần tử từ chỉ mục 1 đến chỉ mục 5 [không bao gồm] bằng cách sử dụng bước 2list_=[10,20,30,40,50]
print [list_[0]]#Output:10
print [list_[-1]]#Output:50
print [list_[-2]]#Output:40
3Nguồn hình ảnh. Tác giả
6. Chỉ số ngoài phạm vi.
Các chỉ mục nằm ngoài phạm vi được xử lý một cách duyên dáng khi được sử dụng để cắt.
list_=[10,20,30,40,50]
print [list_[0]]#Output:10
print [list_[-1]]#Output:50
print [list_[-2]]#Output:40
4danh sách cắt lát
Slicing List trả về một danh sách mới chứa các phần tử được yêu cầu
- Bản sao nông
s="Python"
s[0]="j"
#Output:TypeError: 'str' object does not support item assignment
t=[1,2,3]
t[2]=7#Output:TypeError: 'tuple' object does not support item assignment
r=range[5]
r[0]=10
#Output:TypeError: 'range' object does not support item assignment
1list_=[10,20,30,40,50]
print [list_[0]]#Output:10
print [list_[-1]]#Output:50
print [list_[-2]]#Output:40
5Tham khảo câu chuyện của tôi cho bản sao nông
2.
s="Python"
s[0]="j"
#Output:TypeError: 'str' object does not support item assignment
t=[1,2,3]
t[2]=7#Output:TypeError: 'tuple' object does not support item assignment
r=range[5]
r[0]=10
#Output:TypeError: 'range' object does not support item assignment
2-Trả về danh sách mới chứa các phần tử từ chỉ mục 1 đến chỉ mục 3 [không bao gồm]list_=[10,20,30,40,50]
print [list_[0]]#Output:10
print [list_[-1]]#Output:50
print [list_[-2]]#Output:40
63. Bỏ qua chỉ số dừng
s="Python"
s[0]="j"
#Output:TypeError: 'str' object does not support item assignment
t=[1,2,3]
t[2]=7#Output:TypeError: 'tuple' object does not support item assignment
r=range[5]
r[0]=10
#Output:TypeError: 'range' object does not support item assignment
3-Trả về một danh sách mới chứa các phần tử từ chỉ mục đầu tiên cho đến cuối danh sáchlist_=[10,20,30,40,50]
print [list_[0]]#Output:10
print [list_[-1]]#Output:50
print [list_[-2]]#Output:40
74. Chỉ mục bắt đầu bị bỏ qua
s="Python"
s[0]="j"
#Output:TypeError: 'str' object does not support item assignment
t=[1,2,3]
t[2]=7#Output:TypeError: 'tuple' object does not support item assignment
r=range[5]
r[0]=10
#Output:TypeError: 'range' object does not support item assignment
4-Trả về danh sách mới chứa các phần tử từ đầu danh sách cho đến chỉ mục thứ balist_=[10,20,30,40,50]
print [list_[0]]#Output:10
print [list_[-1]]#Output:50
print [list_[-2]]#Output:40
85. Cắt lát trả về một danh sách mới nhưng lập chỉ mục chỉ trả về mục
s="Python"
s[0]="j"
#Output:TypeError: 'str' object does not support item assignment
t=[1,2,3]
t[2]=7#Output:TypeError: 'tuple' object does not support item assignment
r=range[5]
r[0]=10
#Output:TypeError: 'range' object does not support item assignment
5-Trả về danh sách mới chứa phần tử từ chỉ mục 1. ns="Python"
s[0]="j"
#Output:TypeError: 'str' object does not support item assignment
t=[1,2,3]
t[2]=7#Output:TypeError: 'tuple' object does not support item assignment
r=range[5]
r[0]=10
#Output:TypeError: 'range' object does not support item assignment
6-Trả về phần tử ở chỉ mục 1list_=[10,20,30,40,50]
print [list_[0]]#Output:10
print [list_[-1]]#Output:50
print [list_[-2]]#Output:40
96. bước sử dụng
s="Python"
s[0]="j"
#Output:TypeError: 'str' object does not support item assignment
t=[1,2,3]
t[2]=7#Output:TypeError: 'tuple' object does not support item assignment
r=range[5]
r[0]=10
#Output:TypeError: 'range' object does not support item assignment
7-Trả về danh sách mới chứa phần tử từ chỉ mục 1 đến chỉ mục 5 [không bao gồm] bằng cách sử dụng bước 2#Indexing - tuple
t=[1,2,3,4,5]
print [t[0]]#Output:1
print [t[-1]]#Output:5
print [t[-2]]#Output:4
#Indexing - range object
r=range[5]
print [r[0]]#Output:0
print [r[-1]]#Output:4
print [r[-2]]#Output:3
0Nhiệm vụ cắt látCũng có thể gán cho các lát và điều này thậm chí có thể thay đổi kích thước của danh sách hoặc xóa hoàn toàn
- Trong khi gán để cắt, không cần chỉ định cùng một số phần tử. Chúng ta có thể gán bất kỳ số lượng phần tử nào. Điều này thậm chí sẽ thay đổi kích thước của danh sách
#Indexing - tuple
t=[1,2,3,4,5]
print [t[0]]#Output:1
print [t[-1]]#Output:5
print [t[-2]]#Output:4
#Indexing - range object
r=range[5]
print [r[0]]#Output:0
print [r[-1]]#Output:4
print [r[-2]]#Output:3
12. Chúng ta phải chỉ gán iterable chứ không phải là một phần tử đơn lẻ. Nó sẽ tăng TypeError
#Indexing - tuple
t=[1,2,3,4,5]
print [t[0]]#Output:1
print [t[-1]]#Output:5
print [t[-2]]#Output:4
#Indexing - range object
r=range[5]
print [r[0]]#Output:0
print [r[-1]]#Output:4
print [r[-2]]#Output:3
23. Chúng ta phải gán một iterable[list/tuple/string] để cắt
#Indexing - tuple
t=[1,2,3,4,5]
print [t[0]]#Output:1
print [t[-1]]#Output:5
print [t[-2]]#Output:4
#Indexing - range object
r=range[5]
print [r[0]]#Output:0
print [r[-1]]#Output:4
print [r[-2]]#Output:3
34. Xóa/xóa các phần tử bằng cách gán cho cắt
#Indexing - tuple
t=[1,2,3,4,5]
print [t[0]]#Output:1
print [t[-1]]#Output:5
print [t[-2]]#Output:4
#Indexing - range object
r=range[5]
print [r[0]]#Output:0
print [r[-1]]#Output:4
print [r[-2]]#Output:3
45. Xóa danh sách bằng cách gán danh sách trống cho s[. ]
#Indexing - tuple
t=[1,2,3,4,5]
print [t[0]]#Output:1
print [t[-1]]#Output:5
print [t[-2]]#Output:4
#Indexing - range object
r=range[5]
print [r[0]]#Output:0
print [r[-1]]#Output:4
print [r[-2]]#Output:3
5lát cắt
Một đối tượng thường chứa một phần của
s="Python"
8. Một lát cắt được tạo bằng cách sử dụng ký hiệu chỉ số dưới,
s[0]="j"
#Output:TypeError: 'str' object does not support item assignment
t=[1,2,3]
t[2]=7#Output:TypeError: 'tuple' object does not support item assignment
r=range[5]
r[0]=10
#Output:TypeError: 'range' object does not support item assignments="Python"
9 với dấu hai chấm giữa các số khi một số được đưa ra, chẳng hạn như trong
s[0]="j"
#Output:TypeError: 'str' object does not support item assignment
t=[1,2,3]
t[2]=7#Output:TypeError: 'tuple' object does not support item assignment
r=range[5]
r[0]=10
#Output:TypeError: 'range' object does not support item assignmentt=[1,2,[3]]
0. Ký hiệu dấu ngoặc [chỉ số dưới] sử dụng các đối tượng lát bên trong. -Tài liệu Python
#Item assignment supported for list element inside tuple.
t[2][0]=7
print [t]#Output:[1, 2, [7]]
trình xây dựng lát cắt
#Indexing - tuple
t=[1,2,3,4,5]
print [t[0]]#Output:1
print [t[-1]]#Output:5
print [t[-2]]#Output:4
#Indexing - range object
r=range[5]
print [r[0]]#Output:0
print [r[-1]]#Output:4
print [r[-2]]#Output:3
6Trả về một đối tượng lát đại diện cho tập hợp các chỉ số được chỉ định bởi
t=[1,2,[3]]
#Item assignment supported for list element inside tuple.
t[2][0]=7
print [t]#Output:[1, 2, [7]]
1. Đối số bắt đầu và bước mặc định là t=[1,2,[3]]
#Item assignment supported for list element inside tuple.
t[2][0]=7
print [t]#Output:[1, 2, [7]]
2#Indexing - tuple
t=[1,2,3,4,5]
print [t[0]]#Output:1
print [t[-1]]#Output:5
print [t[-2]]#Output:4
#Indexing - range object
r=range[5]
print [r[0]]#Output:0
print [r[-1]]#Output:4
print [r[-2]]#Output:3
7Sử dụng đối tượng lát trong cú pháp mở rộng chỉ mục
#Indexing - tuple
t=[1,2,3,4,5]
print [t[0]]#Output:1
print [t[-1]]#Output:5
print [t[-2]]#Output:4
#Indexing - range object
r=range[5]
print [r[0]]#Output:0
print [r[-1]]#Output:4
print [r[-2]]#Output:3
8đảo []
Tạo một trình vòng lặp trả về các phần tử đã chọn từ vòng lặp. Nếu bắt đầu là
t=[1,2,[3]]
3, thì quá trình lặp lại bắt đầu từ 0. Nếu bước là
#Item assignment supported for list element inside tuple.
t[2][0]=7
print [t]#Output:[1, 2, [7]]t=[1,2,[3]]
3, thì bước này sẽ mặc định là một. Nếu điểm dừng là
#Item assignment supported for list element inside tuple.
t[2][0]=7
print [t]#Output:[1, 2, [7]]t=[1,2,[3]]
3, thì quá trình lặp tiếp tục cho đến khi hết trình lặp, nếu có; .
#Item assignment supported for list element inside tuple.
t[2][0]=7
print [t]#Output:[1, 2, [7]]t=[1,2,[3]]
6 không hỗ trợ các giá trị âm cho bắt đầu, dừng và bước. -Tài liệu Python
#Item assignment supported for list element inside tuple.
t[2][0]=7
print [t]#Output:[1, 2, [7]]Các chuỗi trong Python có thể được cắt lát không?
Chuỗi Python hỗ trợ cắt để tạo chuỗi con . Lưu ý rằng chuỗi Python là bất biến, việc cắt sẽ tạo ra một chuỗi con mới từ chuỗi nguồn và chuỗi gốc không thay đổi.Các chuỗi có thể được lập chỉ mục trong Python không?
Trong Python, các chuỗi được sắp xếp theo trình tự dữ liệu ký tự và do đó có thể được lập chỉ mục theo cách này . Có thể truy cập các ký tự riêng lẻ trong một chuỗi bằng cách chỉ định tên chuỗi theo sau là một số trong dấu ngoặc vuông [ [] ].Lập chỉ mục và cắt trong chuỗi Python là gì?
“Lập chỉ mục” có nghĩa là đề cập đến một phần tử của một iterable theo vị trí của nó trong iterable. “Cắt lát” có nghĩa là lấy một tập hợp con các phần tử từ một lần lặp dựa trên các chỉ số của chúng .Cắt lát có thể được thực hiện trên chuỗi không?
Cắt chuỗi . Chỉ định chỉ mục bắt đầu và chỉ mục kết thúc, được phân tách bằng dấu hai chấm, để trả về một phần của chuỗi. You can return a range of characters by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string.