python定义函数

def get_2d_list_of_vowels_count(list_of_lists):

img

img

def get_2d_list_of_vowels_count(list_of_lists):
    return list(map(lambda x: [len([j for j in i if j in 'aeiou']) for i in x], list_of_lists))

res = get_2d_list_of_vowels_count([['fish', 'barrel', 'like'], ['apple', 'orange']])
print(res)
--result
[[1, 2, 2], [2, 3]]

def get_2d_list_of_vowels_count(list_of_lists):
    # 不知道是不是只嵌套2层
    res = []  # 结果列表
    vowels = ['a', 'i', 'o', 'u', 'e']  # 元音列表
    for l in list_of_lists:
        res.append([])  # 每遍历1次就添加一个列表
        for word in l:  # 遍历每个单词
            vowel_count = 0  # 元音数量
            for char in word:  # 遍历每个单词的字母
                if char in vowels:
                    vowel_count += 1
            res[-1].append(vowel_count)  # 在最后面的列表添加元音数量
    return res