简体   繁体   English

Python字符串比较-元音

[英]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). 我正在尝试编写一个将两个字符串作为参数并返回它们是否具有相同的元音(数量无关紧要)的Python函数。

Therefore ('indeed','bed') should return true, but ('indeed','irate') should return false. 因此('indeed','bed')应该返回true,但是('indeed','irate')应该返回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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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