搜索列表中的字符串字符

时间:2018-10-12 17:04:22

标签: python string list search

我必须搜索一个单词是否没有元音,然后将它们放在另一个列表中。 我无法使其正常运行,我也不明白为什么。

    for i in range(len(Cadena)):
    if all Vocales[] not in Cadena:
        Lista.append(Cadena[i])

4 个答案:

答案 0 :(得分:0)

尝试一下。

word = 'apple'
vowels = ['a','e','i','o','u','A','E','I','O','U']
for letter in word:
    if letter in vowels:
        #this word has a vowel, do something
    else:
        # this word doesn't have a vowel.

答案 1 :(得分:0)

我不确定100%表示变量的含义,但是您的问题很简单。

这是解决方案:

# array with words that you to sort into two groups
words = ["abc", "def", " ggg"]
vowels = ["a", "e", " i", "o", " u"] # possibly include "y"
numVowels = len(vowels)

withVowels = []   # words with vowels
withoutVowels = [] # words without vowels

# categorize words
for w in words:
    for i, v in enumerate(vowels):
        if v in w:
            withVowels.append(w)
        elif i == (numVowels -1):
            withoutVowels.append(w)

在此for循环结束时,withVowels将包含["abc", "def"],而noVowels将包含[" ggg"]

答案 2 :(得分:0)

import re
def vocales(text):
    #with regular expression.
    print re.findall("[aeiouÁÉÍÓÚ]", text.lower(), re.IGNORECASE)
    #or verifying letter by letter
    print [e for e in text if e in "aeiouÁÉÍÓÚ"]
    #Shows the characters that are not vowels
    print [e for e in text if e not in "aeiouÁÉÍÓÚ"]
    #Returns false if it has vowels
    return not len([e for e in text if e in "aeiouÁÉÍÓÚ"])

vocales("Hola mundo. Hello world")

输出:

['o', 'a', 'u', 'o', 'e', 'o', 'o']
['o', 'a', 'u', 'o', 'e', 'o', 'o']
['H', 'l', ' ', 'm', 'n', 'd', '.', ' ', 'H', 'l', 'l', ' ', 'w', 'r', 'l', 'd']
False

答案 3 :(得分:0)

您可以使用所有元音设置一个字符串,然后可以使用列表理解功能对照此字符串检查您的单词

vow = 'aeiou' 
words = ['apple', 'banana', 'vsh', 'stmpd']

w_vowels = [i for i in words if any(k in vow for k in i)]
wo_vowels = [i for i in words if not any(k in vow for k in i)]

print(w_vowels) # => ['apple', 'banana']
print(wo_vowels) # => ['vsh', 'stmpd']

没有any的扩展循环:

w_vowels = []
for i in words:
    for k in i:
        if k in vow:
            w_vowels.append(i)
            break
wo_vowels = []
for i in words:
    for k in i:
        if k in vow:
            break
    else:
        wo_vowels.append(i)