Python - 字串中母音索引
Python 是一種廣泛使用的程式語言,用於各種目的,例如 Web 開發、資料科學、機器學習以及執行許多不同的自動化流程。在本文中,我們將探討如何使用一些 Python 功能從給定文字中提取母音索引。
在字串中查詢母音索引的不同方法
For 迴圈
在這種方法中,只需檢查字串中的每個元素,我們就可以確定它是否為母音並記錄其索引。我們可以透過以下語法和示例更好地理解它:
示例
def vowel_indices_in_a_string(whole_string): # The function vowel_indices_in_a_string is given the string as input vowels = "aeiou" # All the vowels are defined in the variable vowel_indices = [] # This empty list will store the indices of the vowels in the string for index, char in enumerate(whole_string): # enumerate will help us in finding the indices and the character from the string given as input if char.lower() in vowels: # Only the lower characters of the vowels will be considered with the help of if function vowel_indices.append(index) # All the indices of the vowels in the string will be stored in this list return vowel_indices main_string = "They are a really good group of programmers" # String is given as input final_indices = vowel_indices_in_a_string(main_string) # The function vowel_indices_in_a_string is run print(final_indices)
輸出
上述示例的輸出如下:
[2, 5, 7, 9, 12, 13, 19, 20, 25, 26, 29, 34, 37, 40] # The numbering starts from 0 and space in between words is also considered as 1 indices
列表推導式
使用此技術,將使用現有列表為字串中存在的母音索引建立一個新列表。對於此需求,列表推導式是一種利用現有列表建立新列表的優秀方法。讓我們舉個例子來更好地理解它:
示例
def vowel_indices_in_a_string(whole_string): # The function vowel_indices_in_a_string is given the string as input vowels = "aeiou" # The vowels are defined in a variable return [index for index, char in enumerate(whole_string) if char.lower() in vowels] # With the help of list comprehension we check each element present in the string and with the help of if statement we will consider only the lower case characters #Example main_string = "They are a really good group of programmers" # String is given as input final_indices = vowel_indices_in_a_string(main_string) # The function vowel_indices_in_a_string is run print(final_indices)
輸出
上述示例的輸出如下:
[2, 5, 7, 9, 12, 13, 19, 20, 25, 26, 29, 34, 37, 40] # The numbering starts from 0 and space in between words is also considered as 1 indices
正則表示式模組
透過構建模式並找到模式中描述的確切字母,我們能夠在正則表示式模組的支援下快速識別字符串中母音的索引。讓我們舉個例子來更好地理解它:
示例
import re # Do not forget to import re module or else error might occur def vowel_indices_in_a_string(whole_string): # The function vowel_indices_in_a_string is given the string as input list_comprehension_pattern = r'[aeiou]' # A new pattern is created and all the vowels are defined in it vowel_indices = [m.start() for m in re.finditer(list_comprehension_pattern, whole_string, re.IGNORECASE)] # The re.finditer function will help us to find all the vowels in the string with the help of the pattern and re.ignorecase will not consider the case of the character return vowel_indices #Example main_string = "They are a really good group of programmers" # String is given as input final_indices = vowel_indices_in_a_string(main_string) # The function vowel_indices_in_a_string is run print(final_indices)
輸出
示例輸出如下:
[2, 5, 7, 9, 12, 13, 19, 20, 25, 26, 29, 34, 37, 40] # The numbering starts from 0 and space in between words is also considered as 1 indices
filter 函式
在這種方法中,我們將使用 lambda 函式和 filter 函式,簡單地從給定的輸入字串中篩選出所需元素的索引。讓我們舉個例子來更好地理解它
示例
def all_vowel(char): # A function called all_vowel is created vowels = "aeiou" # All the vowels are defined in the function return char.lower() in vowels def vowel_indices_in_a_string(whole_string):# The function vowel_indices_in_a_string is given the string as input vowel_indices = list(filter(lambda x: all_vowel(whole_string[x]), range(len(whole_string)))) # The range function is used to create the indices of all the characters. Filter function is used to apply the all_vowel function to each index in the sequence generated by the range function and lambda x: is used to check if the specific element is a vowel and list will convert the whole output into a list return vowel_indices #Example main_string = "They are a really good group of programmers" # String is given as input final_indices = vowel_indices_in_a_string(main_string) # The function vowel_indices_in_a_string is run print(final_indices)
輸出
上述示例的輸出如下:
[2, 5, 7, 9, 12, 13, 19, 20, 25, 26, 29, 34, 37, 40] # The numbering starts from 0 and space in between words is also considered as 1 indices
Numpy 庫
這是一種複雜的方法,因為 numpy 庫主要用於輸入中存在數值資料的情況,但如果正確使用,它也可以有效地用於這種情況。讓我們舉個例子來更好地理解它:
示例
import numpy as np # Do not forget to import numpy or else error might occur def vowel_indices_in_a_string(whole_string):# The function vowel_indices_in_a_string is given the string as input vowels = "aeiou" # The vowels are defined with the help of a variable lowercase_input = whole_string.lower() # All the characters are converted into lower case so that all the characters are treated equally all_vowel = np.array([char in vowels for char in lowercase_input]) # An array is created with the help of np.array using a list comprehension. Each element will check if the corresponding element is a vowel or not vowel_indices = list(np.where(all_vowel)[0]) #np.where function will help us to find the indices of the vowels detected in the input string return vowel_indices #Example main_string = "They are a really good group of programmers" # String is given as input final_indices = vowel_indices_in_a_string(main_string) # The function vowel_indices_in_a_string is run print(final_indices)
輸出
上述示例的輸出如下:
[2, 5, 7, 9, 12, 13, 19, 20, 25, 26, 29, 34, 37, 40]
結論
掌握查詢給定字串中母音索引的不同方法對於成為一名高效的程式設計師非常重要。可以參考本文中提供的方法,並根據方便選擇上述任何一種方法。
廣告