-
-
Save bbookman/7d09a66ffdec60d5a55d167751f64175 to your computer and use it in GitHub Desktop.
''' | |
Create a list of all the consonants in the string "Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams" | |
''' | |
sentence = "Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams" | |
result = [letter for letter in sentence if letter not in 'a,e,i,o,u, " "'] |
Why could fail? Works very well for me like this:
stringex4 = "Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams" print(list (a for a in stringex4 if a not in ('a','e','i','o','u', ' ') ) )
And I have a similar code
Add special characters to your stringex4 and verify that it is going to add the special characters like they were consonants.
@gonzalezloaizar You're right, I understand, complementing the solution of the author, I added to the conditions this to support special characters:
import string
stringex4 = "Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams, special characters: * \' and ()/"
print(list (a for a in stringex4 if a not in ('a','e','i','o','u', ' ') and (a in list(string.ascii_lowercase) or a in list(string.ascii_uppercase)) ) )
Great 😃👍🏻
List Comprehension: Consonants in a string
sentence = "Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams"
consonant_list = [letter for letter in sentence if letter not in 'a, e, i, o, u, " "']
print(consonant_list)
Try this one
string="Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams, special characters: * ' and ()/"
print([char for char in string if ('A'<=char<='Z' or 'a'<=char<='z') and char not in 'aeiouAEIOU'])
It will work for all kind of string including numeric and special characters
What is wrong with my code below?
text = 'Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams'
vowel = ['a','e','i','o','u']
p = [i for i in text for v in vowel if v not in i.lower()]
print(p)
What is wrong with my code below?
text = 'Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams' vowel = ['a','e','i','o','u'] p = [i for i in text for v in vowel if v not in i.lower()] print(p)
Hey there, let's look at this together!
Basically, you're saying "for each character in text, for each vowel in vowels, if the vowel is not in the lowercase version of the already lower-case vowels then add it to my list, p.
There's really two things we want to change here.
First, we only need the "for each character in text..." portion. Let's update only this for now:
p = [i for i in text if i not in i.lower()]
Okay sweet, so now if the character i is not in i.lower it'll add it to the list, p. Of course, this doesn't really make sense, so we need to change the last part. Let's break it down.
So i here is being used to hold the character that we are currently on as it iterates. We are wanting to check to see if this character is in your list, vowel. We also want to account for the case where the current character is uppercase, and preferably make it lowercase for our comparison. Let's do this now!
p = [i for i in text if i.lower() not in vowel]
This should now do what we want it to do!
A small side note, don't forget that this problem is asking for only the consonants. We may additionally want to ignore the spaces:
p = [i for i in text if i.lower() not in (vowel + [' '])]
I hope this helps!
I would just propose to transforme the list in a set to have a unique count of each different consonnant :
IN [ ] :
some_string = "Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams"
voyelles = ["a", "e", "i", "o", "u", "y", " "]
consonnant = [c for c in some_string.lower() if c not in voyelles]
print(set(consonnant))
OUT[ ] : {'g', 'r', 'l', 'w', 'd', 'k', 'h', 's', 'm', 't', 'n'}
vowels_space = ['a', 'e', 'i', 'o', 'u', ' ']
sentence = 'Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams'
my_list = [letter for letter in sentence.lower() if letter not in vowels_space]
sentence = 'Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams'
non_consonants = "aeioau"
consonants = [con for con in sentence.lower() if (con.isalpha() and con not in non_consonants)]
sentence = "Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams"
counting_consonants = [len([digits for digits in sentence if digits != 'a'
or digits != 'e'
or digits != 'o'
or digits != 'u'])]
print(counting_consonants)
print([cons for cons in text_str if cons.strip() and cons not in "aeiou"])
This could fail if the sentence contains special characters.