仅在dict中显示特定值

时间:2016-11-14 19:12:08

标签: python python-3.x

text = 'hello'
vowels = 'aeiou'

如何制作它以便打印出类似的元素,"元音a出现x次"

2 个答案:

答案 0 :(得分: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}

答案 1 :(得分:0)

你可以这样做:

void OnTriggerEnter(Collider other) 
{
    other.transform.position = spawnPoint.position;
}