Finding first vowel of a word in Python
Your code always return len(word)-1
if the character in position 1
is not a vowel. Also elephant
didn't work and spa
works only because of the bug I mentioned, it returns 2
which is len(word)-1
and not the index of the vowel found. Try debug your code line by line and you'll figure it out very quickly.
This could be a working code that return -1
if there's no vowel, else return the index of the first vowel found.
def find_first_vowel(word):
i = 0
while i < len(word):
if word[i] in vowels:
return i
i += 1
return -1
EDIT
If you want to return the last character if there's no vowels just change return -1
with return len(word) - 1
. Here:
def find_first_vowel(word):
i = 0
while i < len(word):
if word[i] in vowels:
return i
i += 1
return len(word) - 1
How do you locate/check the letter of each word in a file to see if it is a vowel or consonant?
A string, alike any other sequence in Python, is subscriptable.
So for a word, you can get the first letter by doing word[0]
Then, from other other post you already know, how to check if it is vowel or consonant.
You can do that for every word in your text by looping over them.
words_starting_with_vowels = []
words_starting_with_consonants = []
vowels = ['a', 'e', 'i', 'o', 'u']
for word in text: # loop over all words
lower_case_letter = word[0].lower()
if lower_case_letter in vowels:
words_starting_with_vowels.append(word)
else:
words_starting_with_consonants.append(word)
How do you find out how many vowels are in a word?
def up_it(word):
vowel =['a','e','i','o','u']
count =0
for i in vowel:
count+=word.count(i)
return count
Count Vowels from a word list and return the number as a list
You are feeding a list but your logic is appropriate for a single string only:
number_of_vowels('Bean') # [2]
You need to either adjust the input to your function, or modify your function to calculate the number of vowels for each element of your list. Since you wish the output to be a list of numbers, I assume you're looking for the second option.
To do this, just add an extra for
clause in your list comprehension:
def number_of_vowels(words):
return [len([letter for letter in word if letter.lower() in vowels]) \
for word in words]
number_of_vowels(names) # [1, 2, 2, 3, 2, 0, 3]
However, note the intermediary lists are not required. You can use sum
with a generator expression instead, taking advantage of the fact True == 1
and False == 0
:
vowels = set('aeiou')
def number_of_vowels(words):
return [sum(letter.lower() in vowels for letter in word) for word in words]
number_of_vowels(names) # [1, 2, 2, 3, 2, 0, 3]
Notice a couple of further changes to improve your algorithm:
- Check for
letter.lower() in vowels
instead of just creating a list of lower-case letters. Otherwise vowels as capital letters, e.g. in "April", will be ignored. - Convert your vowels to
set
for O(1) lookup complexity.
How to find number of vowels in a list of words?
Your error is that count needs to be initializes inside the loop:
you also can add a part where you count both upper case and lower case vowels.
wordsList = ["creativity", "anna", "civic", "apology", "refer", "mistress", "rotor", "mindset"]
dictionary = {}
vowels = "aeoui"
for word in wordsList:
count = 0 # I moved it
for letter in word:
if letter.lower() in vowels: # to consider both upper/lower case
count = count + 1
dictionary[word] = (count)
print(dictionary)
Related Topics
Regex to Match Digits and At Most One Space Between Them
Convert Numpy.Datetime64 to String Object in Python
Get All Rows That Have Same Value in Pandas
Python Number With 1000 Separator
Python Opencv - Add Alpha Channel to Rgb Image
How to Get String Objects Instead of Unicode from Json
Add Excel File Attachment When Sending Python Email
How to Transform Floats to Integers in a List
How to Convert Np.Int64 into Python Int64 for Pandasseries
Setting Matplotlib Colorbar Range
How to Make a Grade Calculator in Python
Django.Db.Utils.Operationalerror: (1045, Access Denied for User '<User>'@'Localhost'
Stripping Non Printable Characters from a String in Python
In Python, How to Check If a String Only Contains Certain Characters