Python Forum
Return not vowels list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return not vowels list
#1
Hey there !
Since today , I've been trying to write a program doing this thing below :

'''
Create a program that takes some text and returns a list of all the characters in the text that are not vowels,
sorted in alphabetical order

and using some sets in it . 
'''
I actually tried some way of doing this but unfortunately didn't work :

getString = input('Please enter something : ')
# ConvertedString = set(getString)
# print(type(ConvertedString))
print(getString)


vowels = 'A', 'E', 'I', 'O', 'U'
notVowels = []

for i in getString:
    for x in vowels:
        if i == x:
            print('Sorry')

for y in getString:
    for c in vowels:
        if y not in c:
            notVowels.append(y)
            print(notVowels)
and also
def recognize_vowels(vowels, consonants):
    for i in getString:
        if vowels not in i:
            consonants = list(getString)
            print(vowels)
        return vowels
        return consonants


recognize_vowels({'A', 'E', 'I', 'O', 'U'}, getString)
After all , yet I'm unable to get the output that I want Huh
So , I'll be very happy if you can possibly help me solving this problem .
Thanks .
Reply
#2
In your current code:
Your first outer loop (line 10-13, second code block) writes out 'Sorry' for every uppercase vowel in your string (it ignores 'a' but catches 'A')

Your second outer loop (line 15-19) adds a letter to the list multiple times. The reason for this is the loop on line 16 which makes you examine every letter many times. Try to replace this with an 'if' statement instead testing if your character is in the vowels set or not.

In your last code block you have an error. You can use the 'in' operator with two strings to check if the first string is in the other. However, 'vowels' is not a string, you can check its type with
print(type(vowels))
.

Hope this helps you a step closer.
Reply
#3
Maybe it's good idea to clarify what you want to accomplish and based on that write some code.

As it appears:

1. Input is string
2. Output is list of consonants in input string sorted in alphabetical order
3. Set must be used

So, in process we need somehow use set. Set is unordered collection of unique elements.

In general terms our steps can be: extract unique characters from string, filter out vowels and sort result.

How to get unique characters from string? One approach can be:

>>> s = 'What a wonderful world'
>>> set(s)
{' ', 'W', 'a', 'd', 'e', 'f', 'h', 'l', 'n', 'o', 'r', 't', 'u', 'w'}
All unique characters nicely extracted. But, as one can observe, there is problem with capital letters ('W' and 'w') and space (' ') - we need to handle those in order to get correct output.

To solve capital letter(s) problem why not feed set function with lowercase string:

>>> set(s.lower())
{' ', 'a', 'd', 'e', 'f', 'h', 'l', 'n', 'o', 'r', 't', 'u', 'w'}


Nice! Now we need to filter out vowels and space. There are many ways to achieve it. It can be done with comprehension or with set method:

>>> vowels = 'aeiou'
>>> set(s.lower()).difference(set(vowels))
{' ', 'd', 'f', 'h', 'l', 'n', 'r', 't', 'w'} 
We are free of vowels but have space. We can 'cheat' and add space to vowels:

>>> vowels = 'aeiou '
>>> set(s.lower()).difference(set(vowels))
{'d', 'f', 'h', 'l', 'n', 'r', 't', 'w'}
We are getting closer. Now we need to sort. As built-in function sorted takes any iterable and returns list then we can just apply it. To be extremely concise we condense it into oneliner:

>>> sorted(set(s.lower()).difference(set('aeiou ')))                   
['d', 'f', 'h', 'l', 'n', 'r', 't', 'w']
Input is string - check
Output is list of consonants in alphabetical order - check
sets are used - check

NB! One should be aware of this behaviour:

>>> s = "It's a wonderful world"
>>> sorted(set(s.lower()).difference(set('aeiou ')))                   
["'", 'd', 'f', 'l', 'n', 'r', 's', 't', 'w']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,356 Sep-23-2022, 07:53 AM
Last Post: deanhystad
Bug How to print only vowels from an input? PP9044 8 7,427 Feb-26-2021, 04:02 PM
Last Post: Serafim
  Return the sum of the first n numbers in the list. pav1983 3 3,956 Jun-24-2020, 03:37 AM
Last Post: deanhystad
  Counting Vowels in Python sapphosvoice 1 3,248 May-05-2020, 04:24 AM
Last Post: buran
  Methods that return the highest score from the list erfanakbari1 7 7,056 Mar-26-2019, 08:32 PM
Last Post: aankrose
  Help with finding vowels nlord7 1 2,230 Feb-27-2019, 04:40 AM
Last Post: ichabod801
  Functions to remove vowels and extra blanks RiceGum 10 5,785 Nov-17-2017, 05:40 PM
Last Post: heiner55
  Calling a function to return a list of percentages Liquid_Ocelot 7 6,230 Mar-25-2017, 01:20 PM
Last Post: Larz60+
  maximum and minimum element from the list and return output in dict MeeranRizvi 1 3,701 Jan-02-2017, 02:12 PM
Last Post: ichabod801
  Print the index of the vowels in a string MeeranRizvi 4 14,593 Dec-29-2016, 02:43 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020