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

Tôi đang đọc tệp, vậy làm thế nào để tôi có được lượt hoặc chỉ mục cho mỗi dòng?

Show
for line in excelRead:
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]

Vì vậy, khi tôi đọc dòng đầu tiên của mình trong Excel, biến dòng bằng với giá trị dòng trong Excel, nhưng làm thế nào để tôi tìm thấy đó là lượt nào?

Hỏi ngày 21 tháng 4 năm 2014 lúc 22:11Apr 21, 2014 at 22:11

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

Sử dụng

for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index
5:

for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index

Tham số

for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index
6 cho
for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index
7 chỉ ra rằng bạn muốn bắt đầu chỉ mục ở mức 1, thay vì 0.

Đã trả lời ngày 21 tháng 4 năm 2014 lúc 22:17Apr 21, 2014 at 22:17

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

Shaktimaanshaktimaanshaktimaan

11.7K2 Huy hiệu vàng28 Huy hiệu bạc33 Huy hiệu đồng2 gold badges28 silver badges33 bronze badges

Try:

for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]

for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index
8 bởi vì theo mặc định, nó bắt đầu tại
for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index
9, nhưng hàng đầu tiên của Excel là hàng
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
0.

Bạn có thể tìm hiểu thêm về chức năng

for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index
7 tích hợp ở đây.

Đã trả lời ngày 21 tháng 4 năm 2014 lúc 22:16Apr 21, 2014 at 22:16

s16hs16hs16h

4.4471 Huy hiệu vàng19 Huy hiệu bạc32 Huy hiệu đồng1 gold badge19 silver badges32 bronze badges

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.

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.

text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7


Is

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức Python Index () với sự trợ giúp của các ví dụ.

str.index(sub[, start[, end]] )

Phương thức for index, line in enumerate(excelRead, start=1): # use index as you wish keywrds=[] title=line.split("+") title=[lines.strip()for lines in title] print title[0] 2 trả về chỉ mục của một chuỗi con bên trong chuỗi (nếu tìm thấy). Nếu không tìm thấy chất nền, nó sẽ tăng một ngoại lệ.

Thí dụ

  • chỉ mục () cú pháp - substring to be searched in the string str.
  • Đó là cú pháp là: and end(optional) - substring is searched within str[start:end]

chỉ mục () tham số

  • Phương thức
    for index, line in enumerate(excelRead, start=1):
        # use index as you wish
        keywrds=[]
        title=line.split("+")
        title=[lines.strip()for lines in title]
        print title[0]
    
    2 lấy ba tham số:
  • Sub - Subring sẽ được tìm kiếm trong chuỗi str.ValueError exception.

Bắt đầu và kết thúc (Tùy chọn) - Subring được tìm kiếm trong STR [Bắt đầu: End]

index () giá trị trả về-1 if the substring is not found, whereas

for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
2 throws an exception.


Nếu chuỗi con tồn tại bên trong chuỗi, nó sẽ trả về chỉ số thấp nhất trong chuỗi nơi tìm thấy chuỗi con.

sentence = 'Python programming is fun.'

result = sentence.index('is fun')

print("Substring 'is fun':", result)

result = sentence.index('Java')

print("Substring 'Java':", result)

Nếu chất nền không tồn tại bên trong chuỗi, nó sẽ tăng ngoại lệ giá trị.

Substring 'is fun': 19

Traceback (most recent call last):
  File "<string>", line 6, in 
    result = sentence.index('Java')
ValueError: substring not found

Phương thức

for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
2 tương tự như phương thức Find () cho các chuỗi. Index in Python starts from 0 and not 1. So the occurrence is 19 and not 20.


Sự khác biệt duy nhất là phương thức tìm () trả về -1 nếu không tìm thấy chuỗi con, trong khi for index, line in enumerate(excelRead, start=1): # use index as you wish keywrds=[] title=line.split("+") title=[lines.strip()for lines in title] print title[0] 2 ném một ngoại lệ.

sentence = 'Python programming is fun.'

# Substring is searched in 'gramming is fun.'

print(sentence.index('ing', 10))

# Substring is searched in 'gramming is ' print(sentence.index('g is', 10, -4)) # Substring is searched in 'programming'

print(sentence.index('fun', 7, 18))

Nếu chất nền không tồn tại bên trong chuỗi, nó sẽ tăng ngoại lệ giá trị.

15
17
Traceback (most recent call last):
  File "<string>", line 10, in 
    print(quote.index('fun', 7, 18))
ValueError: substring not found

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

for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
6
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
7
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
8
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
9____________
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
1
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
030 ____33
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
0
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
9________________
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
7
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
8

text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
9
str.index(sub[, start[, end]] )
0
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
1
str.index(sub[, start[, end]] )
2

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

str.index(sub[, start[, end]] )
3
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
7
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
8
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
0____________
str.index(sub[, start[, end]] )
8
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
0
sentence = 'Python programming is fun.'

result = sentence.index('is fun')

print("Substring 'is fun':", result)

result = sentence.index('Java')

print("Substring 'Java':", result)
03030522223030

text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
9
Substring 'is fun': 19

Traceback (most recent call last):
  File "<string>", line 6, in 
    result = sentence.index('Java')
ValueError: substring not found
5
sentence = 'Python programming is fun.'

result = sentence.index('is fun')

print("Substring 'is fun':", result)

result = sentence.index('Java')

print("Substring 'Java':", result)
2
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
0
sentence = 'Python programming is fun.'

result = sentence.index('is fun')

print("Substring 'is fun':", result)

result = sentence.index('Java')

print("Substring 'Java':", result)
2
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
0
sentence = 'Python programming is fun.'

# Substring is searched in 'gramming is fun.'

print(sentence.index('ing', 10))

# Substring is searched in 'gramming is ' print(sentence.index('g is', 10, -4)) # Substring is searched in 'programming'

print(sentence.index('fun', 7, 18))

0
str.index(sub[, start[, end]] )
2

Đầu ra: & nbsp; 

for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index
0

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;

for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index
1

Python3

str.index(sub[, start[, end]] )
3
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
7
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
8
sentence = 'Python programming is fun.'

# Substring is searched in 'gramming is fun.'

print(sentence.index('ing', 10))

# Substring is searched in 'gramming is ' print(sentence.index('g is', 10, -4)) # Substring is searched in 'programming'

print(sentence.index('fun', 7, 18))

5
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
0
sentence = 'Python programming is fun.'

# Substring is searched in 'gramming is fun.'

print(sentence.index('ing', 10))

# Substring is searched in 'gramming is ' print(sentence.index('g is', 10, -4)) # Substring is searched in 'programming'

print(sentence.index('fun', 7, 18))

0
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
0
Substring 'is fun': 19

Traceback (most recent call last):
  File "<string>", line 6, in 
    result = sentence.index('Java')
ValueError: substring not found
2__________
sentence = 'Python programming is fun.'

# Substring is searched in 'gramming is fun.'

print(sentence.index('ing', 10))

# Substring is searched in 'gramming is ' print(sentence.index('g is', 10, -4)) # Substring is searched in 'programming'

print(sentence.index('fun', 7, 18))

5
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
030

text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
9
Substring 'is fun': 19

Traceback (most recent call last):
  File "<string>", line 6, in 
    result = sentence.index('Java')
ValueError: substring not found
5
sentence = 'Python programming is fun.'

# Substring is searched in 'gramming is fun.'

print(sentence.index('ing', 10))

# Substring is searched in 'gramming is ' print(sentence.index('g is', 10, -4)) # Substring is searched in 'programming'

print(sentence.index('fun', 7, 18))

5
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
0
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
0
str.index(sub[, start[, end]] )
2

Output:

for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index
2

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ị, khi phần tử tìm kiếm không có trong danh sách.

Python3

str.index(sub[, start[, end]] )
3
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
7
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
8
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
0____________
str.index(sub[, start[, end]] )
8
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
0
sentence = 'Python programming is fun.'

result = sentence.index('is fun')

print("Substring 'is fun':", result)

result = sentence.index('Java')

print("Substring 'Java':", result)
03030522223030

text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
9
Substring 'is fun': 19

Traceback (most recent call last):
  File "<string>", line 6, in 
    result = sentence.index('Java')
ValueError: substring not found
5
for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index
16
str.index(sub[, start[, end]] )
2

Output:  

for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index
3

Đầ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;

str.index(sub[, start[, end]] )
3
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
7
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
8
sentence = 'Python programming is fun.'

# Substring is searched in 'gramming is fun.'

print(sentence.index('ing', 10))

# Substring is searched in 'gramming is ' print(sentence.index('g is', 10, -4)) # Substring is searched in 'programming'

print(sentence.index('fun', 7, 18))

5
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
0
sentence = 'Python programming is fun.'

# Substring is searched in 'gramming is fun.'

print(sentence.index('ing', 10))

# Substring is searched in 'gramming is ' print(sentence.index('g is', 10, -4)) # Substring is searched in 'programming'

print(sentence.index('fun', 7, 18))

0
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
0
Substring 'is fun': 19

Traceback (most recent call last):
  File "<string>", line 6, in 
    result = sentence.index('Java')
ValueError: substring not found
2__________
sentence = 'Python programming is fun.'

# Substring is searched in 'gramming is fun.'

print(sentence.index('ing', 10))

# Substring is searched in 'gramming is ' print(sentence.index('g is', 10, -4)) # Substring is searched in 'programming'

print(sentence.index('fun', 7, 18))

5
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
030

for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index
38
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
9
for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index
40

Output:

for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index
4

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ị, 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;

str.index(sub[, start[, end]] )
3
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
7
for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
8
sentence = 'Python programming is fun.'

# Substring is searched in 'gramming is fun.'

print(sentence.index('ing', 10))

# Substring is searched in 'gramming is ' print(sentence.index('g is', 10, -4)) # Substring is searched in 'programming'

print(sentence.index('fun', 7, 18))

5
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
0
sentence = 'Python programming is fun.'

# Substring is searched in 'gramming is fun.'

print(sentence.index('ing', 10))

# Substring is searched in 'gramming is ' print(sentence.index('g is', 10, -4)) # Substring is searched in 'programming'

print(sentence.index('fun', 7, 18))

0
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
0
Substring 'is fun': 19

Traceback (most recent call last):
  File "<string>", line 6, in 
    result = sentence.index('Java')
ValueError: substring not found
2__________
sentence = 'Python programming is fun.'

# Substring is searched in 'gramming is fun.'

print(sentence.index('ing', 10))

# Substring is searched in 'gramming is ' print(sentence.index('g is', 10, -4)) # Substring is searched in 'programming'

print(sentence.index('fun', 7, 18))

5
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
030

for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index
38
text = 'Python is fun'

# find the index of is result = text.index('is')

print(result) # Output: 7
9
for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index
40