Python 字符串中的元音索引
Python是一种广泛使用的编程语言,用于各种不同的用途,如网页开发、数据科学、机器学习和自动化执行许多不同的流程。在本文中,我们将探讨如何使用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
筛选函数
在这个方法中,我们将通过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]
结论
了解不同的方法来查找给定字符串中元音字母的索引是成为高效程序员的重要知识。可以参考上述文章中提供的方法,并根据需要使用其中的任何方法。