Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức Danh sách Python [] với sự trợ giúp của các ví dụ.
Phương thức
list.index[element, start, end]4 Trả về chỉ mục của phần tử được chỉ định trong danh sách.
Thí dụ
animals = ['cat', 'dog', 'rabbit', 'horse']
# get the index of 'dog'
index = animals.index['dog']
print[index]
# Output: 1
Cú pháp của chỉ mục danh sách []
Cú pháp của phương thức danh sách
list.index[element, start, end]4 là:
list.index[element, start, end]
Danh sách chỉ mục [] tham số
Phương thức danh sách
list.index[element, start, end]4 có thể mất tối đa ba đối số:
- Phần tử - Phần tử được tìm kiếm - the element to be searched
- Bắt đầu [Tùy chọn] - Bắt đầu tìm kiếm từ chỉ mục này [optional] - start searching from this index
- End [Tùy chọn] - Tìm kiếm phần tử lên đến chỉ mục này [optional] - search the element up to this index
Trả về giá trị từ chỉ mục danh sách []
- Phương thức
list.index[element, start, end]
4 trả về chỉ mục của phần tử đã cho trong danh sách. - Nếu phần tử không được tìm thấy, ngoại lệ
list.index[element, start, end]
8 sẽ được nâng lên.
Lưu ý: Phương thức list.index[element, start, end]
4 chỉ trả về lần xuất hiện đầu tiên của phần tử khớp. The
list.index[element, start, end]4 method only returns the first occurrence of the matching element.
Ví dụ 1: Tìm chỉ mục của phần tử
# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# index of 'e' in vowels
index = vowels.index['e']
print['The index of e:', index]
# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index['i']
print['The index of i:', index]
Đầu ra
The index of e: 1 The index of i: 2
Ví dụ 2: Chỉ mục của phần tử không có trong danh sách
# vowels list
vowels = ['a', 'e', 'i', 'o', 'u']
# index of 'p' is vowels
index = vowels.index['p']
print['The index of p:', index]
Đầu ra
ValueError: 'p' is not in list
Ví dụ 2: Chỉ mục của phần tử không có trong danh sách
# alphabets list
alphabets = ['a', 'e', 'i', 'o', 'g', 'l', 'i', 'u']
# index of 'i' in alphabets
index = alphabets.index['e'] # 1
print['The index of e:', index]
# 'i' after the 4th index is searched
index = alphabets.index['i', 4] # 6
print['The index of i:', index]
# 'i' between 3rd and 5th index is searched
index = alphabets.index['i', 3, 5] # Error!
print['The index of i:', index]
Đầu ra
The index of e: 1 The index of i: 6 Traceback [most recent call last]: File "*lt;string>", line 13, in ValueError: 'i' is not in list
Python Index [] là một hàm sẵn có trong Python, tìm kiếm một phần tử nhất định từ đầu danh sách và trả về chỉ số của lần xuất hiện đầu tiên. & NBSP;is an inbuilt function in Python, which searches for a given element from the start of the list and returns the index of the first occurrence.
Cách tìm chỉ mục của một phần tử hoặc các mục trong danh sách
Trong bài viết này, chúng tôi sẽ trình bày các ví dụ khác nhau để tìm chỉ mục, chẳng hạn như:
- Tìm chỉ mục của phần tử
- Hoạt động của Index [] với các tham số bắt đầu và kết thúc
- Hoạt động của chỉ mục [] chỉ với hai tham số
- Chỉ mục của phần tử không có trong danh sách
- Cách khắc phục danh sách chỉ mục ra khỏi phạm vi
Phương thức cú pháp của chỉ mục []
Cú pháp: list_name.index [phần tử, bắt đầu, kết thúc] & nbsp;list_name.index[element, start, end]
Parameters:
- Phần tử - Phần tử có chỉ số thấp nhất sẽ được trả về. – The element whose lowest index will be returned.
- Bắt đầu [Tùy chọn] - Vị trí từ nơi tìm kiếm bắt đầu. [Optional] – The position from where the search begins.
- kết thúc [tùy chọn] - vị trí từ nơi tìm kiếm kết thúc. [Optional] – The position from where the search ends.
Trả về: Trả về chỉ số thấp nhất nơi phần tử xuất hiện. Returns the lowest index where the element appears.
Lỗi: Nếu bất kỳ yếu tố nào không có mặt được tìm kiếm, nó sẽ tăng giá trịerror. If any element which is not present is searched, it raises a ValueError.
Ví dụ 1: Tìm chỉ mục của phần tử: Find the index of the element
Tìm chỉ mục của ‘Bat, sử dụng index [] trên danh sách python
Python3
# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# index of 'e' in vowels
index = vowels.index['e']
print['The index of e:', index]
# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index['i']
print['The index of i:', index]
0____21 # vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# index of 'e' in vowels
index = vowels.index['e']
print['The index of e:', index]
# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index['i']
print['The index of i:', index]
22____23# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# index of 'e' in vowels
index = vowels.index['e']
print['The index of e:', index]
# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index['i']
print['The index of i:', index]
4# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# index of 'e' in vowels
index = vowels.index['e']
print['The index of e:', index]
# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index['i']
print['The index of i:', index]
5# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# index of 'e' in vowels
index = vowels.index['e']
print['The index of e:', index]
# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index['i']
print['The index of i:', index]
4# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# index of 'e' in vowels
index = vowels.index['e']
print['The index of e:', index]
# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index['i']
print['The index of i:', index]
7# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# index of 'e' in vowels
index = vowels.index['e']
print['The index of e:', index]
# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index['i']
print['The index of i:', index]
4# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# index of 'e' in vowels
index = vowels.index['e']
print['The index of e:', index]
# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index['i']
print['The index of i:', index]
3# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# index of 'e' in vowels
index = vowels.index['e']
print['The index of e:', index]
# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index['i']
print['The index of i:', index]
4The index of e: 1 The index of i: 21
The index of e: 1 The index of i: 22
The index of e: 1 The index of i: 23
The index of e: 1 The index of i: 24
# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# index of 'e' in vowels
index = vowels.index['e']
print['The index of e:', index]
# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index['i']
print['The index of i:', index]
5The index of e: 1 The index of i: 26
Output:
1
Ví dụ 2: Hoạt động của Index [] với các tham số bắt đầu và kết thúcWorking of the index[] With Start and End Parameters
& nbsp; Trong ví dụ này, chúng tôi tìm thấy một phần tử trong danh sách python, chỉ số của một phần tử 4 ở giữa chỉ số ở vị trí thứ 4 và kết thúc với vị trí thứ 8. & nbsp;the index at the 4th position and ending with the 8th position.
Python3
Các
The index of e: 1 The index of i: 23
ValueError: 'p' is not in list9
# vowels list
vowels = ['a', 'e', 'i', 'o', 'u']
# index of 'p' is vowels
index = vowels.index['p']
print['The index of p:', index]
6# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# index of 'e' in vowels
index = vowels.index['e']
print['The index of e:', index]
# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index['i']
print['The index of i:', index]
4# vowels list
vowels = ['a', 'e', 'i', 'o', 'u']
# index of 'p' is vowels
index = vowels.index['p']
print['The index of p:', index]
6# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# index of 'e' in vowels
index = vowels.index['e']
print['The index of e:', index]
# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index['i']
print['The index of i:', index]
4# alphabets list
alphabets = ['a', 'e', 'i', 'o', 'g', 'l', 'i', 'u']
# index of 'i' in alphabets
index = alphabets.index['e'] # 1
print['The index of e:', index]
# 'i' after the 4th index is searched
index = alphabets.index['i', 4] # 6
print['The index of i:', index]
# 'i' between 3rd and 5th index is searched
index = alphabets.index['i', 3, 5] # Error!
print['The index of i:', index]
4The index of e: 1 The index of i: 26
Đầu ra: & nbsp;
7
Ví dụ 3: Hoạt động của chỉ mục [] chỉ với hai tham sốWorking of the index[] With two Parameters only
Trong ví dụ này, chúng ta sẽ thấy khi chúng ta vượt qua hai đối số trong hàm chỉ mục, đối số đầu tiên được coi là phần tử sẽ được tìm kiếm và đối số thứ hai là chỉ mục từ nơi bắt đầu tìm kiếm. & NBSP;
list.index[element, start, end]0
Python3
Các
The index of e: 1 The index of i: 23
ValueError: 'p' is not in list9
# alphabets list
alphabets = ['a', 'e', 'i', 'o', 'g', 'l', 'i', 'u']
# index of 'i' in alphabets
index = alphabets.index['e'] # 1
print['The index of e:', index]
# 'i' after the 4th index is searched
index = alphabets.index['i', 4] # 6
print['The index of i:', index]
# 'i' between 3rd and 5th index is searched
index = alphabets.index['i', 3, 5] # Error!
print['The index of i:', index]
9# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# index of 'e' in vowels
index = vowels.index['e']
print['The index of e:', index]
# element 'i' is searched
# index of the first 'i' is returned
index = vowels.index['i']
print['The index of i:', index]
4# vowels list
vowels = ['a', 'e', 'i', 'o', 'u']
# index of 'p' is vowels
index = vowels.index['p']
print['The index of p:', index]
0The index of e: 1 The index of i: 26
Output:
list.index[element, start, end]1
Ví dụ 4: Chỉ mục của phần tử không có trong danh sáchIndex of the Element not Present in the List
Chỉ số danh sách Python [] tăng giá trịerror, khi phần tử tìm kiếm không có trong danh sách.
Python3
Các
The index of e: 1 The index of i: 23
ValueError: 'p' is not in list9
list.index[element, start, end]10
The index of e: 1 The index of i: 26
Output:
list.index[element, start, end]2
Đầu ra: & nbsp;
Ví dụ 3: Hoạt động của chỉ mục [] chỉ với hai tham số
Python3
Trong ví dụ này, chúng ta sẽ thấy khi chúng ta vượt qua hai đối số trong hàm chỉ mục, đối số đầu tiên được coi là phần tử sẽ được tìm kiếm và đối số thứ hai là chỉ mục từ nơi bắt đầu tìm kiếm. & NBSP;
Các
list.index[element, start, end]32
The index of e: 1 The index of i: 23
list.index[element, start, end]34
Output:
list.index[element, start, end]3
Ví dụ 4: Chỉ mục của phần tử không có trong danh sáchThe length of the list is 5 and if we are an iterating list on 6 then it will generate the error.
Chỉ số danh sách Python [] tăng giá trịerror, khi phần tử tìm kiếm không có trong danh sách.
Ví dụ 5: Cách khắc phục danh sách chỉ mục ra khỏi phạm vi bằng chỉ mục []
Python3
Trong ví dụ này, chúng ta sẽ thấy khi chúng ta vượt qua hai đối số trong hàm chỉ mục, đối số đầu tiên được coi là phần tử sẽ được tìm kiếm và đối số thứ hai là chỉ mục từ nơi bắt đầu tìm kiếm. & NBSP;
Các
list.index[element, start, end]32
The index of e: 1 The index of i: 23
list.index[element, start, end]34