2016-11-14 42 views
0
text = 'hello' 
vowels = 'aeiou' 

具體數值我怎麼做,以便它打印像「的元音發生x次」只顯示在字典

+0

查找最小值,然後打印匹配的條目。 – stark

+0

然後你不需要預先加載這些默認值的字典。 –

回答

0

你可以這樣來做:

frequent_vowels = {k:v for k,v in vowels.items() if v != 0} 
print(frequent_vowels) 
1

我會採取稍微不同的方法,只爲字符串中存在的元音實例化字典鍵。

text = 'hello' 
vowels = 'aeiou' 

text_dict = {} 
for char in text.lower(): 
    if char in vowels: 
     text_dict[char] = text_dict.get(char, 0) + 1 

min_count = min(text_dict.values()) 
minimum_dict = {k: v for k, v in text_dict.items() if v == min_count} 

print(minimum_dict) # {'e': 1, 'o': 1}