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

With:

sentence= input("Enter a sentence")
keyword= input("Input a keyword from the sentence")

Tôi muốn tìm vị trí của từ khóa trong câu. Cho đến nay, tôi có mã này loại bỏ dấu chấm câu và làm cho tất cả các chữ cái viết thường:

punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''#This code defines punctuation
#This code removes the punctuation
no_punct = "" 
for char in sentence:
   if char not in punctuations:
       no_punct = no_punct + char

no_punct1 =(str.lower (no_punct)

Tôi biết cần một đoạn mã thực sự tìm thấy vị trí của từ.

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

Mazdak

103K18 Huy hiệu vàng158 Huy hiệu bạc183 Huy hiệu đồng18 gold badges158 silver badges183 bronze badges

Hỏi ngày 10 tháng 10 năm 2015 lúc 11:48Oct 10, 2015 at 11:48

1

Đây là những gì

punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''#This code defines punctuation
#This code removes the punctuation
no_punct = "" 
for char in sentence:
   if char not in punctuations:
       no_punct = no_punct + char

no_punct1 =(str.lower (no_punct)
1 dành cho:

sentence.find(word)

Điều này sẽ cung cấp cho bạn vị trí bắt đầu của từ (nếu nó tồn tại, nếu không thì -1), thì bạn chỉ có thể thêm độ dài của từ vào nó để có được chỉ số kết thúc của nó.

start_index = sentence.find(word)
end_index = start_index + len(word) # if the start_index is not -1

Đã trả lời ngày 10 tháng 10 năm 2015 lúc 11:50Oct 10, 2015 at 11:50

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

MazdakmazdakMazdak

103K18 Huy hiệu vàng158 Huy hiệu bạc183 Huy hiệu đồng18 gold badges158 silver badges183 bronze badges

1

Hỏi ngày 10 tháng 10 năm 2015 lúc 11:48

words = sentence.split(' ')
if keyword in words:
    pos = words.index(keyword)

Đây là những gì

punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''#This code defines punctuation
#This code removes the punctuation
no_punct = "" 
for char in sentence:
   if char not in punctuations:
       no_punct = no_punct + char

no_punct1 =(str.lower (no_punct)
1 dành cho:

EDIT::

Điều này sẽ cung cấp cho bạn vị trí bắt đầu của từ (nếu nó tồn tại, nếu không thì -1), thì bạn chỉ có thể thêm độ dài của từ vào nó để có được chỉ số kết thúc của nó.

Đã trả lời ngày 10 tháng 10 năm 2015 lúc 11:50Oct 10, 2015 at 11:58

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

Mazdakmazdakpaolo

Nếu với vị trí bạn có nghĩa là từ thứ n trong câu, bạn có thể làm như sau:3 gold badges17 silver badges25 bronze badges

1

Điều này sẽ phân chia câu sau mỗi lần xuất hiện của một không gian và lưu câu trong một danh sách (khôn ngoan hơn). Nếu câu chứa từ khóa, list.index () sẽ tìm thấy vị trí của nó.

Câu lệnh IF là cần thiết để đảm bảo từ khóa nằm trong câu, nếu không thì danh sách.index () sẽ tăng giá trị.

Thí dụ

text = 'Python is fun'

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

print(result) # Output: 7


chỉ mục () cú pháp

Đó là cú pháp là:

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

chỉ mục () tham số

Phương thức

punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''#This code defines punctuation
#This code removes the punctuation
no_punct = "" 
for char in sentence:
   if char not in punctuations:
       no_punct = no_punct + char

no_punct1 =(str.lower (no_punct)
2 lấy ba tham số:

  • Sub - Subring sẽ được tìm kiếm trong chuỗi str. - substring to be searched in the string str.
  • Bắt đầu và kết thúc (Tùy chọn) - Subring được tìm kiếm trong STR [Bắt đầu: End] and end(optional) - substring is searched within str[start:end]

index () giá trị trả về

  • 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.
  • 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ị.ValueError exception.

Phương thức

punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''#This code defines punctuation
#This code removes the punctuation
no_punct = "" 
for char in sentence:
   if char not in punctuations:
       no_punct = no_punct + char

no_punct1 =(str.lower (no_punct)
2 tương tự như phương thức Find () cho các chuỗi.

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

punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''#This code defines punctuation
#This code removes the punctuation
no_punct = "" 
for char in sentence:
   if char not in punctuations:
       no_punct = no_punct + char

no_punct1 =(str.lower (no_punct)
2 ném một ngoại lệ.-1 if the substring is not found, whereas
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''#This code defines punctuation
#This code removes the punctuation
no_punct = "" 
for char in sentence:
   if char not in punctuations:
       no_punct = no_punct + char

no_punct1 =(str.lower (no_punct)
2 throws an exception.


Ví dụ 1: Chỉ mục () chỉ với đối số phụ

sentence = 'Python programming is fun.'

result = sentence.index('is fun')

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

result = sentence.index('Java')

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

Đầu ra

Substring 'is fun': 19

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

Lưu ý: Chỉ mục trong Python bắt đầu từ 0 chứ không phải 1. Vì vậy, sự xuất hiện là 19 và không 20. Index in Python starts from 0 and not 1. So the occurrence is 19 and not 20.


Ví dụ 2: index () với các đối số bắt đầu và kết thúc

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

Đầu ra

punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''#This code defines punctuation
#This code removes the punctuation
no_punct = "" 
for char in sentence:
   if char not in punctuations:
       no_punct = no_punct + char

no_punct1 =(str.lower (no_punct)
0