Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在列表Python中查找以元音开头的第一个字符串_Python_Python 3.x - Fatal编程技术网

在列表Python中查找以元音开头的第一个字符串

在列表Python中查找以元音开头的第一个字符串,python,python-3.x,Python,Python 3.x,我是python的新手- 我必须构建一个名为“first\uu元音”的函数。接受字符串列表作为输入 并返回以小写元音开头的第一个字符串(“a”、“e”、“i”、“o”或“u”)。如果没有以元音开头的字符串,则返回空字符串(“”) 你能帮我建立这个功能吗 谢谢您需要: def first_vowel(lst): # iterate over list using for loop for i in lst: # check if first letter is vo

我是python的新手-

我必须构建一个名为“first\uu元音”的函数。接受字符串列表作为输入 并返回以小写元音开头的第一个字符串
(“a”、“e”、“i”、“o”或“u”)
。如果没有以元音开头的字符串,则返回空字符串
(“”)

你能帮我建立这个功能吗

谢谢

您需要:

def first_vowel(lst):
    # iterate over list using for loop
    for i in lst:
        # check if first letter is vowel
        if i and i[0] in ['a','e','i','o','u']:
            return i 
    return ""

k = ['sad','dad','mad','asd','eas']
print(first_vowel(k))
或者也可以使用
regex

import re

def first_vow(lst):
    pat = re.compile(r'^[aeiou][a-zA-Z]*')
    for i in lst:
        match = re.match(pat, lst)
        if match:
            return i
    return ""

k = ['sad','Aad','mad','','asd','eas']
first_vow(k)
选中此项:

vowels = ["a", "e", "i", "o", "u"]

def first_vowel(ss):
    for s in ss:
        if s and s[0] in vowels:
            return s
    return ""
测试:


您也可以始终使用
str.startswith()
函数:

def first_vowel(lst):
    for elem in lst:
        if elem.startswith(('a','e','i','o','u')):
            return elem
    return ''
k = ['', 'b','sad','dad','mad','asd','eas']
print(first_vowel(k))

这是有效的。非常感谢。s[0]在语法中带“and”的意思是什么?@FHamad
如果s
表示字符串不为空
简单明了
s[0]
是字符串中的第一个字符。我建议您选择一个简单的Python课程,例如,在@ FHamad,如果这有助于考虑把这个答案标记为接受(如果你不介意)。谢谢我用了这个答案。有没有办法标记为已接受?请告诉我怎么做,这个网站的新手。@FHamad在我的答案旁边的
3
下面有一个V标记。请点击V标记,它会像这样变成绿色
def first_vowel(lst):
    for elem in lst:
        if elem.startswith(('a','e','i','o','u')):
            return elem
    return ''
k = ['', 'b','sad','dad','mad','asd','eas']
print(first_vowel(k))
l1 = ['hi', 'there', 'its', 'an' , 'answer', '']

def first_vowel(var):
    for i in var:
        if i.startswith(('a', 'e', 'i', 'o', 'u')): return i


first_vowel(l1) # output-> 'its'