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]

结论

掌握查找给定字符串中元音索引的不同方法对于成为一名高效的程序员非常重要。可以参考本文中提供的方法,并根据方便选择上述任何一种方法。

更新于:2023年8月7日

283 次浏览

启动您的职业生涯

完成课程后获得认证

开始学习
广告