Hướng dẫn how do you find the index range in python? - làm thế nào để bạn tìm thấy phạm vi chỉ mục trong python?

Đây là cách bạn có thể truy cập các chỉ số và các yếu tố của mảng bằng các vòng lặp cho trong.

Show

1. Các phần tử lặp với toán tử bộ đếm và items = [8, 23, 45, 12, 78] for i in enumerate(items): print("index/value", i) 4.

items = [8, 23, 45, 12, 78]
counter = 0

for value in items:
    print(counter, value)
    counter += 1

Result:

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78

2. Các phần tử lặp bằng phương pháp items = [8, 23, 45, 12, 78] for i in enumerate(items): print("index/value", i) 5.

items = [8, 23, 45, 12, 78]

for i in enumerate(items):
    print("index/value", i)

Result:

#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)

3. Sử dụng riêng biệt items = [8, 23, 45, 12, 78] for i in enumerate(items): print("index/value", i) 6 và items = [8, 23, 45, 12, 78] for i in enumerate(items): print("index/value", i) 7.

items = [8, 23, 45, 12, 78]

for index, value in enumerate(items):
    print("index", index, "for value", value)

Result:

#    index 0 for value 8
#    index 1 for value 23
#    index 2 for value 45
#    index 3 for value 12
#    index 4 for value 78

4. Bạn có thể thay đổi số items = [8, 23, 45, 12, 78] for i in enumerate(items): print("index/value", i) 6 thành bất kỳ mức tăng nào.

items = [8, 23, 45, 12, 78]

for i, value in enumerate(items, start=1000):
    print(i, value)

Result:

#    1000 8
#    1001 23
#    1002 45
#    1003 12
#    1004 78

5. Tăng phản ứng tự động với items = [8, 23, 45, 12, 78] for i in enumerate(items): print("index/value", i) 9.

items = [8, 23, 45, 12, 78]

for i in range(len(items)):
    print("Index:", i, "Value:", items[i])

Result:

#    ('Index:', 0, 'Value:', 8)
#    ('Index:', 1, 'Value:', 23)
#    ('Index:', 2, 'Value:', 45)
#    ('Index:', 3, 'Value:', 12)
#    ('Index:', 4, 'Value:', 78)

6. Sử dụng vòng lặp for-in bên trong chức năng.

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
0

Result:

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78

7. Tất nhiên, chúng ta không thể quên vòng lặp # index/value (0, 8) # index/value (1, 23) # index/value (2, 45) # index/value (3, 12) # index/value (4, 78) 0.

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
2

Result:

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78

8. Tuyên bố # index/value (0, 8) # index/value (1, 23) # index/value (2, 45) # index/value (3, 12) # index/value (4, 78) 1 Trả về một đối tượng máy phát.

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
4

Result:

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
5

9. Biểu thức nội tuyến với vòng lặp và # index/value (0, 8) # index/value (1, 23) # index/value (2, 45) # index/value (3, 12) # index/value (4, 78) 2.

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
6

Result:

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
7

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

#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
3
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
4
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
5
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
6
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
7
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
8
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
7
items = [8, 23, 45, 12, 78]

for index, value in enumerate(items):
    print("index", index, "for value", value)
0
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
7
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
6
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
7
items = [8, 23, 45, 12, 78]

for index, value in enumerate(items):
    print("index", index, "for value", value)
445

items = [8, 23, 45, 12, 78]

for index, value in enumerate(items):
    print("index", index, "for value", value)
6
items = [8, 23, 45, 12, 78]

for index, value in enumerate(items):
    print("index", index, "for value", value)
7
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
8
items = [8, 23, 45, 12, 78]

for index, value in enumerate(items):
    print("index", index, "for value", value)
9

Output:    

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
8

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

items = [8, 23, 45, 12, 78]

for index, value in enumerate(items):
    print("index", index, "for value", value)
6
#    1000 8
#    1001 23
#    1002 45
#    1003 12
#    1004 78
2
#    index 0 for value 8
#    index 1 for value 23
#    index 2 for value 45
#    index 3 for value 12
#    index 4 for value 78
9
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
7
#    index 0 for value 8
#    index 1 for value 23
#    index 2 for value 45
#    index 3 for value 12
#    index 4 for value 78
9
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
7
#    1000 8
#    1001 23
#    1002 45
#    1003 12
#    1004 78
7
items = [8, 23, 45, 12, 78]

for index, value in enumerate(items):
    print("index", index, "for value", value)
9

Đầu ra: & nbsp; 

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
9

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;

items = [8, 23, 45, 12, 78]

for i in enumerate(items):
    print("index/value", i)
0

Python3

#    index 0 for value 8
#    index 1 for value 23
#    index 2 for value 45
#    index 3 for value 12
#    index 4 for value 78
0
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
4
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
5
items = [8, 23, 45, 12, 78]

for i in range(len(items)):
    print("Index:", i, "Value:", items[i])
2
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
7__77737373769
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
7__82

items = [8, 23, 45, 12, 78]

for index, value in enumerate(items):
    print("index", index, "for value", value)
6
#    1000 8
#    1001 23
#    1002 45
#    1003 12
#    1004 78
2
items = [8, 23, 45, 12, 78]

for i in range(len(items)):
    print("Index:", i, "Value:", items[i])
2
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
7
#    index 0 for value 8
#    index 1 for value 23
#    index 2 for value 45
#    index 3 for value 12
#    index 4 for value 78
3
items = [8, 23, 45, 12, 78]

for index, value in enumerate(items):
    print("index", index, "for value", value)
9

Output:

items = [8, 23, 45, 12, 78]

for i in enumerate(items):
    print("index/value", i)
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

items = [8, 23, 45, 12, 78]

for index, value in enumerate(items):
    print("index", index, "for value", value)
6
#    1000 8
#    1001 23
#    1002 45
#    1003 12
#    1004 78
2
#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
23
items = [8, 23, 45, 12, 78]

for index, value in enumerate(items):
    print("index", index, "for value", value)
9

Output:  

items = [8, 23, 45, 12, 78]

for i in enumerate(items):
    print("index/value", i)
2

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 ()

Ở đây chúng tôi sẽ tạo một danh sách và sau đó cố gắng lặp lại danh sách bằng cách sử dụng các giá trị không đổi trong các vòng lặp.

Python3

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
25
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
4
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
5
#    index 0 for value 8
#    index 1 for value 23
#    index 2 for value 45
#    index 3 for value 12
#    index 4 for value 78
3
#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
29____55
#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
29
#    index 0 for value 8
#    index 1 for value 23
#    index 2 for value 45
#    index 3 for value 12
#    index 4 for value 78
7
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
7
#    index 0 for value 8
#    index 1 for value 23
#    index 2 for value 45
#    index 3 for value 12
#    index 4 for value 78
9
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
7
items = [8, 23, 45, 12, 78]

for i, value in enumerate(items, start=1000):
    print(i, value)
9
items = [8, 23, 45, 12, 78]

for index, value in enumerate(items):
    print("index", index, "for value", value)
5

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
38
#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
39
#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
40
#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
41
#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
42
items = [8, 23, 45, 12, 78]

for i in range(len(items)):
    print("Index:", i, "Value:", items[i])
2
#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
44

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
45
items = [8, 23, 45, 12, 78]

for index, value in enumerate(items):
    print("index", index, "for value", value)
6
#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
47

Output:

items = [8, 23, 45, 12, 78]

for i in enumerate(items):
    print("index/value", i)
3

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

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
25
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
4
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
5
#    index 0 for value 8
#    index 1 for value 23
#    index 2 for value 45
#    index 3 for value 12
#    index 4 for value 78
3
#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
29____55
#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
29
#    index 0 for value 8
#    index 1 for value 23
#    index 2 for value 45
#    index 3 for value 12
#    index 4 for value 78
7
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
7
#    index 0 for value 8
#    index 1 for value 23
#    index 2 for value 45
#    index 3 for value 12
#    index 4 for value 78
9
#    index/value (0, 8)
#    index/value (1, 23)
#    index/value (2, 45)
#    index/value (3, 12)
#    index/value (4, 78)
7
items = [8, 23, 45, 12, 78]

for i, value in enumerate(items, start=1000):
    print(i, value)
9
items = [8, 23, 45, 12, 78]

for index, value in enumerate(items):
    print("index", index, "for value", value)
5

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
38
#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
39
#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
40
#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
41
#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
42
items = [8, 23, 45, 12, 78]

for i in range(len(items)):
    print("Index:", i, "Value:", items[i])
2
#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
44

#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
45
items = [8, 23, 45, 12, 78]

for index, value in enumerate(items):
    print("index", index, "for value", value)
6
#    0 8
#    1 23
#    2 45
#    3 12
#    4 78
47


Làm thế nào để bạn tìm thấy giá trị chỉ mục trong Python?

Để tạo điều kiện cho điều này, Python có chức năng sẵn có gọi là index (). Hàm này lấy phần tử làm đối số và trả về chỉ mục. Bằng cách sử dụng chức năng này, chúng tôi có thể tìm thấy chỉ mục của một phần tử trong danh sách trong Python.Python has an inbuilt function called index(). This function takes in the element as an argument and returns the index. By using this function we are able to find the index of an element in a list in Python.

Làm thế nào để bạn tạo một phạm vi chỉ mục trong Python?

Các bước để sử dụng hàm phạm vi ()..
Truyền các giá trị bắt đầu và dừng đến phạm vi () ví dụ, phạm vi (0, 6). Ở đây, bắt đầu = 0 và dừng = 6. ....
Chuyển giá trị bước cho phạm vi () Bước chỉ định mức tăng. ....
Sử dụng cho vòng lặp để truy cập từng số. Sử dụng cho vòng lặp để lặp và truy cập một chuỗi các số được trả về bởi một phạm vi () ..

Làm cách nào để sửa phạm vi được lập chỉ mục trong Python?

Python IndexError: Danh sách chỉ mục ra khỏi phạm vi có thể được sửa bằng cách đảm bảo mọi yếu tố được truy cập trong danh sách nằm trong phạm vi chỉ mục của danh sách.Điều này có thể được thực hiện bằng cách sử dụng hàm phạm vi () cùng với hàm Len ().making sure any elements accessed in a list are within the index range of the list. This can be done by using the range() function along with the len() function.

Hàm Syntax cho Range () trong Python là gì?

Cú pháp của hàm python () Chức năng Bắt đầu: [Tùy chọn] Giá trị bắt đầu của chuỗi.Dừng lại: Giá trị tiếp theo sau giá trị cuối của chuỗi.Bước: [Tùy chọn] Giá trị số nguyên, biểu thị sự khác biệt giữa bất kỳ hai số nào trong chuỗi.