简体   繁体   中英

Python String Comparison - Vowels

I'm trying to write a Python function that takes two strings as arguments and returns whether or not they have the same vowels (quantity doesn't matter).

Therefore ('indeed','bed') should return true, but ('indeed','irate') should return false.

I'm stuck with this rather horrifying attempt...

def vocalizer(string_a,string_b):
vowels = ['a', 'e', 'i', 'o', 'u']
result = ''
result_2 = ''
for character in string_a:
    if character in vowels:
       result = result + character
       for item in string_b:
            if item in vowels:
               result_2 = result_2 + item
               for vowel in result:
                    if vowel not in list(result_2):
                       return False
                    else:
                       if vowel in list(result_2):
                          return True

Short and expressive:

def keep_only_vowels(s):
    vowels = ('a', 'e', 'i', 'o', 'u')
    return (c for c in s.lower() if c in vowels)

def vocalizer(s1, s2):
    return set(keep_only_vowels(s1)) == set(keep_only_vowels(s2))

I'd use set intersection to extract the vowels from each string. Like this:

all_vowels = set('aeiou')

def vowel_set(s):
    return all_vowels.intersection(s.lower())

def same_vowels(s1, s2):
    return vowel_set(s1) == vowel_set(s2)

print same_vowels('deed', 'bed')
print same_vowels('indeed', 'irate')
print same_vowels('DEAD', 'earnest')
print same_vowels('blue', 'ICE')

output

True
False 
True
False 

Your own code could be improved very easily, because your general strategy was correct:

def vocalizer(string_a,string_b):
    vowels = ['a', 'e', 'i', 'o', 'u']
    result = ''
    result_2 = ''
    for character in string_a:
       if character in vowels:
           result += character
    for item in string_b:
        if item in vowels:
            result_2 += item

    result = set(list(result))
    result_2 = set(list(result_2))
    #print("result= {}".format(result))
    #print("result_2= {}".format(result_2))
    if result == result_2:
        return True
    else:
        return False

When you strip the words of their consonants, you could simply create a list out of the strings and then delete duplicate elements by converting them to sets. Finally you could compare the sets to see if they are equal. For example:

>>> vocalizer ('indeed','irate')
result= {'i', 'e'}
result_2= {'i', 'e', 'a'}
False
>>> vocalizer ('indeed','ie')
result= {'i', 'e'}
result_2= {'i', 'e'}
True
>>> 

Through re module.

>>> def checkvow(x, y):
        return set(i.lower() for i in re.findall(r'(?i)[aeiou]', x)) == set(i.lower() for i in re.findall(r'(?i)[aeiou]', y))

>>> print(checkvow('deed', 'bed'))
True
>>> print(checkvow('indeed', 'irate'))
False
>>> print(checkvow('DEAD', 'earnest'))
True
>>> print(checkvow('blue', 'ICE'))
False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM