Hướng dẫn how to find the index of a character in a list in python - cách tìm chỉ mục của một ký tự trong danh sách trong python

Đ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

Hướng dẫn how to find the index of a character in a list in python - cách tìm chỉ mục của một ký tự trong danh sách trong python

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 Brissom

Jim 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:09

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)

Hướng dẫn how to find the index of a character in a list in python - cách tìm chỉ mục của một ký tự trong danh sách trong python

Đã trả lời ngày 2 tháng 10 năm 2010 lúc 21:09Mar 6, 2021 at 5:43

Hướng dẫn how to find the index of a character in a list in python - cách tìm chỉ mục của một ký tự trong danh sách trong python

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")
5
a = ["a", "b", "c", "d", "e"]
print a.index("c")
6
a = ["a", "b", "c", "d", "e"]
print a.index("c")
7
a = ["a", "b", "c", "d", "e"]
print a.index("c")
8
a = ["a", "b", "c", "d", "e"]
print a.index("c")
9
for position,char in enumerate(['a','b','c','d']):
    if char=='b':
        print position
0
a = ["a", "b", "c", "d", "e"]
print a.index("c")
9
for position,char in enumerate(['a','b','c','d']):
    if char=='b':
        print position
222

for position,char in enumerate(['a','b','c','d']):
    if char=='b':
        print position
8
for position,char in enumerate(['a','b','c','d']):
    if char=='b':
        print position
9
for position,char in enumerate(['a','b','c','d']):
    if char=='b':
        print position
0
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

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

for position,char in enumerate(['a','b','c','d']):
    if char=='b':
        print position
8
7
4
1
1
a = ["a", "b", "c", "d", "e"]
print a.index("c")
9
1
1
a = ["a", "b", "c", "d", "e"]
print a.index("c")
9
7
9
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)
2
a = ["a", "b", "c", "d", "e"]
print a.index("c")
6
a = ["a", "b", "c", "d", "e"]
print a.index("c")
7
list_name.index(element, start)
4
a = ["a", "b", "c", "d", "e"]
print a.index("c")
9
7
9
a = ["a", "b", "c", "d", "e"]
print a.index("c")
9
7
1
a = ["a", "b", "c", "d", "e"]
print a.index("c")
9
list_name.index(element, start)
444

for position,char in enumerate(['a','b','c','d']):
    if char=='b':
        print position
8
7
4
list_name.index(element, start)
4
a = ["a", "b", "c", "d", "e"]
print a.index("c")
9
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)
5
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

Output:

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
8
7
4
a = ["a", "b", "c", "d", "e"]
print a.index("c")
05
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

Output:  

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")
07
a = ["a", "b", "c", "d", "e"]
print a.index("c")
6
a = ["a", "b", "c", "d", "e"]
print a.index("c")
7
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)
5
a = ["a", "b", "c", "d", "e"]
print a.index("c")
11______37

a = ["a", "b", "c", "d", "e"]
print a.index("c")
20
a = ["a", "b", "c", "d", "e"]
print a.index("c")
21
a = ["a", "b", "c", "d", "e"]
print a.index("c")
22
a = ["a", "b", "c", "d", "e"]
print a.index("c")
23
a = ["a", "b", "c", "d", "e"]
print a.index("c")
24
list_name.index(element, start)
4
a = ["a", "b", "c", "d", "e"]
print a.index("c")
26

a = ["a", "b", "c", "d", "e"]
print a.index("c")
27
for position,char in enumerate(['a','b','c','d']):
    if char=='b':
        print position
8
a = ["a", "b", "c", "d", "e"]
print a.index("c")
29

Output:

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")
07
a = ["a", "b", "c", "d", "e"]
print a.index("c")
6
a = ["a", "b", "c", "d", "e"]
print a.index("c")
7
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)
5
a = ["a", "b", "c", "d", "e"]
print a.index("c")
11______37

a = ["a", "b", "c", "d", "e"]
print a.index("c")
20
a = ["a", "b", "c", "d", "e"]
print a.index("c")
21
a = ["a", "b", "c", "d", "e"]
print a.index("c")
22
a = ["a", "b", "c", "d", "e"]
print a.index("c")
23
a = ["a", "b", "c", "d", "e"]
print a.index("c")
47
a = ["a", "b", "c", "d", "e"]
print a.index("c")
48
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)
5__150

a = ["a", "b", "c", "d", "e"]
print a.index("c")
27
for position,char in enumerate(['a','b','c','d']):
    if char=='b':
        print position
8
a = ["a", "b", "c", "d", "e"]
print a.index("c")
29


Làm thế nào để bạn tìm thấy chỉ số nhân vật trong Python?

Phương pháp 1: Lấy vị trí của một ký tự trong python bằng phương thức rfind () python chuỗi rfind () trả về chỉ số cao nhất của chuỗi con nếu được tìm thấy trong chuỗi đã cho. Nếu không tìm thấy thì nó sẽ trả về -1.rfind() Python String rfind() method returns the highest index of the substring if found in the given string. If not found then it returns -1.

Làm thế nào để bạn tìm thấy chỉ mục của một chuỗi trong danh sách Python?

Bằng cách sử dụng toán tử loại (), chúng ta có thể lấy các chỉ mục phần tử chuỗi từ danh sách, các phần tử chuỗi sẽ thuộc loại str (), vì vậy chúng ta lặp lại toàn bộ danh sách với vòng lặp và trả về chỉ mục loại chuỗi. we can get the string elements indexes from the list, string elements will come under str() type, so we iterate through the entire list with for loop and return the index which is of type string.

Làm thế nào để bạn tìm thấy chỉ mục của một yếu tố trong danh sách các danh sách?

Để tìm cặp chỉ mục (hàng, cột) của một phần tử trong danh sách danh sách, lặp lại trên các hàng và chỉ số của chúng bằng cách sử dụng hàm liệt kê () và sử dụng phương thức Row.index (x) để xác định chỉ số của phần tử X tronghàng .iterate over the rows and their indices using the enumerate() function and use the row. index(x) method to determine the index of element x in the row .

Làm thế nào để bạn tìm thấy chỉ mục của một đối tượng trong danh sách trong Python?

Python - Tìm chỉ mục hoặc vị trí của phần tử trong danh sách.Để tìm chỉ mục về sự xuất hiện đầu tiên của một phần tử trong danh sách Python nhất định, bạn có thể sử dụng phương thức Lớp danh sách Index () với phần tử được truyền làm đối số.Phương thức Index () trả về một số nguyên đại diện cho chỉ mục của trận đấu đầu tiên của phần tử được chỉ định trong danh sách.use index() method of List class with the element passed as argument. The index() method returns an integer that represents the index of first match of specified element in the List.