문자열이 주어졌을 때 모음이 몇 문자인지 분석해 봅시다.
세트 포함
먼저 모든 개별 및 고유 문자를 찾은 다음 모음을 나타내는 문자열에 있는지 테스트합니다.
예시
stringA = "Tutorialspoint is best"
print("Given String: \n",stringA)
vowels = "AaEeIiOoUu"
# Get vowels
res = set([each for each in stringA if each in vowels])
print("The vlowels present in the string:\n ",res) 출력
위의 코드를 실행하면 다음과 같은 결과가 나옵니다. -
Given String:
Tutorialspoint is best
The vlowels present in the string:
{'e', 'i', 'a', 'o', 'u'} fromkey 포함
문자열에서 사전으로 취급하여 모음을 추출할 수 있는 기능입니다.
예시
stringA = "Tutorialspoint is best"
#ignore cases
stringA = stringA.casefold()
vowels = "aeiou"
def vowel_count(string, vowels):
# Take dictionary key as a vowel
count = {}.fromkeys(vowels, 0)
# To count the vowels
for v in string:
if v in count:
# Increasing count for each occurence
count[v] += 1
return count
print("Given String: \n", stringA)
print ("The count of vlowels in the string:\n ",vowel_count(stringA, vowels)) 출력
위의 코드를 실행하면 다음과 같은 결과가 나옵니다. -
Given String:
tutorialspoint is best
The count of vlowels in the string:
{'a': 1, 'e': 1, 'i': 3, 'o': 2, 'u': 1}