Điều gì sẽ là cách tốt nhất để tìm chỉ mục của một ký tự được chỉ định trong danh sách chứa nhiều ký tự?
Đã hỏi ngày 2 tháng 10 năm 2010 lúc 20:48Oct 2, 2010 at 20:48
Một Sandwhicha Sandwhicha sandwhich
4.26412 Huy hiệu vàng41 Huy hiệu bạc61 Huy hiệu Đồng12 gold badges41 silver badges61 bronze badges
>>> ['a', 'b'].index['b']
1
Nếu danh sách đã được sắp xếp, tất nhiên bạn có thể làm tốt hơn tìm kiếm tuyến tính.
Đã trả lời ngày 2 tháng 10 năm 2010 lúc 20:54Oct 2, 2010 at 20:54
2
Có lẽ là phương pháp
a = ["a", "b", "c", "d", "e"]
print a.index["c"]
0?a = ["a", "b", "c", "d", "e"]
print a.index["c"]
Đã trả lời ngày 2 tháng 10 năm 2010 lúc 20:54Oct 2, 2010 at 20:54
Có lẽ là phương pháp
a = ["a", "b", "c", "d", "e"]
print a.index["c"]
0?Jim BrissomJim Brissomjim Brissom3 gold badges37 silver badges33 bronze badges
30.6K3 Huy hiệu vàng37 Huy hiệu bạc33 Huy hiệu Đồng
for position,char in enumerate[['a','b','c','d']]:
if char=='b':
print position
Theo đề xuất của người khác, bạn có thể sử dụng
a = ["a", "b", "c", "d", "e"]
print a.index["c"]
0. Ngoài ra, bạn có thể sử dụng a = ["a", "b", "c", "d", "e"]
print a.index["c"]
2 để có được cả a = ["a", "b", "c", "d", "e"]
print a.index["c"]
0 cũng như a = ["a", "b", "c", "d", "e"]
print a.index["c"]
4Oct 2, 2010 at 21:09def lookallindicesof[char, list]:
indexlist = []
for item in enumerate[list]:
if char in item:
indexlist.append[item[0]]
return indexlist
indexlist = lookllindicesof['o',mylist1]
print[indexlist]
Đã trả lời ngày 2 tháng 10 năm 2010 lúc 21:09Mar 6, 2021 at 5:43
1
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
a = ["a", "b", "c", "d", "e"]
print a.index["c"]
5a = ["a", "b", "c", "d", "e"]
print a.index["c"]
6 a = ["a", "b", "c", "d", "e"]
print a.index["c"]
7a = ["a", "b", "c", "d", "e"]
print a.index["c"]
8a = ["a", "b", "c", "d", "e"]
print a.index["c"]
9for position,char in enumerate[['a','b','c','d']]:
if char=='b':
print position
0a = ["a", "b", "c", "d", "e"]
print a.index["c"]
9for position,char in enumerate[['a','b','c','d']]:
if char=='b':
print position
222for position,char in enumerate[['a','b','c','d']]:
if char=='b':
print position
8for position,char in enumerate[['a','b','c','d']]:
if char=='b':
print position
9for position,char in enumerate[['a','b','c','d']]:
if char=='b':
print position
0def lookallindicesof[char, list]:
indexlist = []
for item in enumerate[list]:
if char in item:
indexlist.append[item[0]]
return indexlist
indexlist = lookllindicesof['o',mylist1]
print[indexlist]
1Output:
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
for position,char in enumerate[['a','b','c','d']]:
if char=='b':
print position
874
11
a = ["a", "b", "c", "d", "e"]
print a.index["c"]
911
a = ["a", "b", "c", "d", "e"]
print a.index["c"]
979
def lookallindicesof[char, list]:
indexlist = []
for item in enumerate[list]:
if char in item:
indexlist.append[item[0]]
return indexlist
indexlist = lookllindicesof['o',mylist1]
print[indexlist]
1Đầ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_name.index[element, start]
Python3
def lookallindicesof[char, list]:
indexlist = []
for item in enumerate[list]:
if char in item:
indexlist.append[item[0]]
return indexlist
indexlist = lookllindicesof['o',mylist1]
print[indexlist]
2a = ["a", "b", "c", "d", "e"]
print a.index["c"]
6 a = ["a", "b", "c", "d", "e"]
print a.index["c"]
7list_name.index[element, start]4
a = ["a", "b", "c", "d", "e"]
print a.index["c"]
979
a = ["a", "b", "c", "d", "e"]
print a.index["c"]
971
a = ["a", "b", "c", "d", "e"]
print a.index["c"]
9list_name.index[element, start]444
for position,char in enumerate[['a','b','c','d']]:
if char=='b':
print position
874
list_name.index[element, start]4
a = ["a", "b", "c", "d", "e"]
print a.index["c"]
9def lookallindicesof[char, list]:
indexlist = []
for item in enumerate[list]:
if char in item:
indexlist.append[item[0]]
return indexlist
indexlist = lookllindicesof['o',mylist1]
print[indexlist]
5def lookallindicesof[char, list]:
indexlist = []
for item in enumerate[list]:
if char in item:
indexlist.append[item[0]]
return indexlist
indexlist = lookllindicesof['o',mylist1]
print[indexlist]
1Output:
3
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
for position,char in enumerate[['a','b','c','d']]:
if char=='b':
print position
874
a = ["a", "b", "c", "d", "e"]
print a.index["c"]
05def lookallindicesof[char, list]:
indexlist = []
for item in enumerate[list]:
if char in item:
indexlist.append[item[0]]
return indexlist
indexlist = lookllindicesof['o',mylist1]
print[indexlist]
1Output:
Traceback [most recent call last]: File "/home/b910d8dcbc0f4f4b61499668654450d2.py", line 8, in print[list1.index[10]] ValueError: 10 is not in list
Đầu ra: & nbsp;
Ví dụ 3: Hoạt động của chỉ mục [] chỉ với hai tham số
Python3
a = ["a", "b", "c", "d", "e"]
print a.index["c"]
07a = ["a", "b", "c", "d", "e"]
print a.index["c"]
6 a = ["a", "b", "c", "d", "e"]
print a.index["c"]
7def lookallindicesof[char, list]:
indexlist = []
for item in enumerate[list]:
if char in item:
indexlist.append[item[0]]
return indexlist
indexlist = lookllindicesof['o',mylist1]
print[indexlist]
5a = ["a", "b", "c", "d", "e"]
print a.index["c"]
11______37a = ["a", "b", "c", "d", "e"]
print a.index["c"]
20 a = ["a", "b", "c", "d", "e"]
print a.index["c"]
21a = ["a", "b", "c", "d", "e"]
print a.index["c"]
22 a = ["a", "b", "c", "d", "e"]
print a.index["c"]
23a = ["a", "b", "c", "d", "e"]
print a.index["c"]
24list_name.index[element, start]4
a = ["a", "b", "c", "d", "e"]
print a.index["c"]
26a = ["a", "b", "c", "d", "e"]
print a.index["c"]
27for position,char in enumerate[['a','b','c','d']]:
if char=='b':
print position
8a = ["a", "b", "c", "d", "e"]
print a.index["c"]
29Output:
1 2 3 4 5 IndexError: list index out of range
Lý do cho lỗi: Độ dài của danh sách là 5 và nếu chúng ta là một danh sách lặp đi lặp lại vào 6 thì nó sẽ tạo ra lỗi.The length of the list is 5 and if we are an iterating list on 6 then it will generate the error.
Giải quyết lỗi này mà không sử dụng Len [] hoặc giá trị không đổi:
Để giải quyết lỗi này, chúng tôi sẽ lấy chỉ số của giá trị cuối cùng của danh sách và sau đó thêm một, sau đó nó sẽ trở thành giá trị chính xác của độ dài.
Python3
a = ["a", "b", "c", "d", "e"]
print a.index["c"]
07a = ["a", "b", "c", "d", "e"]
print a.index["c"]
6 a = ["a", "b", "c", "d", "e"]
print a.index["c"]
7def lookallindicesof[char, list]:
indexlist = []
for item in enumerate[list]:
if char in item:
indexlist.append[item[0]]
return indexlist
indexlist = lookllindicesof['o',mylist1]
print[indexlist]
5a = ["a", "b", "c", "d", "e"]
print a.index["c"]
11______37a = ["a", "b", "c", "d", "e"]
print a.index["c"]
20 a = ["a", "b", "c", "d", "e"]
print a.index["c"]
21a = ["a", "b", "c", "d", "e"]
print a.index["c"]
22 a = ["a", "b", "c", "d", "e"]
print a.index["c"]
23a = ["a", "b", "c", "d", "e"]
print a.index["c"]
47a = ["a", "b", "c", "d", "e"]
print a.index["c"]
48def lookallindicesof[char, list]:
indexlist = []
for item in enumerate[list]:
if char in item:
indexlist.append[item[0]]
return indexlist
indexlist = lookllindicesof['o',mylist1]
print[indexlist]
5__150
a = ["a", "b", "c", "d", "e"]
print a.index["c"]
27for position,char in enumerate[['a','b','c','d']]:
if char=='b':
print position
8a = ["a", "b", "c", "d", "e"]
print a.index["c"]
29