Python程序从列表中打印具有最大元音的元素
给定一个包含字符串元素的列表,任务是编写一个Python程序来打印具有最大元音的字符串。
Input : test_list = [“gfg”, “best”, “for”, “geeks”]
Output : geeks
Explanation : geeks has 2 e’s which is a maximum number of vowels compared to other strings.
Input : test_list = [“gfg”, “best”]
Output : best
Explanation : best has 1 e which is a maximum number of vowels compared to other strings.
方法:使用循环
在这里,我们迭代所有字符串并保留一个计数器来检查每个字符串中元音的数量。然后,在循环结束时返回具有最大元音的字符串。
Python3
# initializing Matrix
test_list = ["gfg", "best", "for", "geeks"]
# printing original list
print("The original list is : " + str(test_list))
res = ""
max_len = 0
for ele in test_list:
# getting maximum length and element
# iteratively
vow_len = len([el for el in ele if el in ['a', 'e', 'o', 'u', 'i']])
if vow_len > max_len:
max_len = vow_len
res = ele
# printing result
print("Maximum vowels word : " + str(res))
输出:
The original list is : [‘gfg’, ‘best’, ‘for’, ‘geeks’]
Maximum vowels word : geeks