Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python - Vowel Indices in String
Finding vowel indices in a string is a common programming task. Python provides multiple approaches including for loops, list comprehension, regular expressions, filter functions, and NumPy arrays.
Using For Loop
The most straightforward approach iterates through each character and checks if it's a vowel ?
def vowel_indices_for_loop(text):
vowels = "aeiou"
vowel_indices = []
for index, char in enumerate(text):
if char.lower() in vowels:
vowel_indices.append(index)
return vowel_indices
# Example
text = "Hello World"
result = vowel_indices_for_loop(text)
print(f"Text: {text}")
print(f"Vowel indices: {result}")
Text: Hello World Vowel indices: [1, 4, 7]
Using List Comprehension
A more concise approach using list comprehension ?
def vowel_indices_list_comp(text):
vowels = "aeiou"
return [index for index, char in enumerate(text) if char.lower() in vowels]
# Example
text = "Python Programming"
result = vowel_indices_list_comp(text)
print(f"Text: {text}")
print(f"Vowel indices: {result}")
Text: Python Programming Vowel indices: [4, 7, 8, 10, 14, 15]
Using Regular Expression
Using the re module with pattern matching ?
import re
def vowel_indices_regex(text):
pattern = r'[aeiou]'
vowel_indices = [match.start() for match in re.finditer(pattern, text, re.IGNORECASE)]
return vowel_indices
# Example
text = "Data Science"
result = vowel_indices_regex(text)
print(f"Text: {text}")
print(f"Vowel indices: {result}")
Text: Data Science Vowel indices: [1, 3, 6, 8, 10]
Using Filter Function
Filtering indices using lambda functions ?
def is_vowel(char):
return char.lower() in "aeiou"
def vowel_indices_filter(text):
vowel_indices = list(filter(lambda x: is_vowel(text[x]), range(len(text))))
return vowel_indices
# Example
text = "Machine Learning"
result = vowel_indices_filter(text)
print(f"Text: {text}")
print(f"Vowel indices: {result}")
Text: Machine Learning Vowel indices: [1, 4, 6, 8, 10, 13, 14]
Using NumPy
Using NumPy arrays for vectorized operations ?
import numpy as np
def vowel_indices_numpy(text):
vowels = "aeiou"
lowercase_text = text.lower()
is_vowel_array = np.array([char in vowels for char in lowercase_text])
vowel_indices = list(np.where(is_vowel_array)[0])
return vowel_indices
# Example
text = "Artificial Intelligence"
result = vowel_indices_numpy(text)
print(f"Text: {text}")
print(f"Vowel indices: {result}")
Text: Artificial Intelligence Vowel indices: [0, 3, 7, 9, 11, 12, 15, 17, 21]
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| For Loop | High | Good | Beginners, clear logic |
| List Comprehension | High | Better | Concise, Pythonic code |
| Regular Expression | Medium | Good | Complex pattern matching |
| Filter Function | Medium | Good | Functional programming style |
| NumPy | Low | Best | Large datasets, numerical work |
Conclusion
List comprehension offers the best balance of readability and performance for most cases. Use NumPy for large datasets and regular expressions for complex pattern matching requirements.
Advertisements
