编写程序
'''
功能:单词按首字母分类
作者:hf
日期:2021.12.4
'''
words = ['apple', 'book', 'bar', 'hat', 'at', 'blade',
'matrix', 'not', 'auto', 'node', 'have', 'bad',
'mummy', 'native', 'hot', 'nation', 'brain']
map = {}
for word in words:
start_letter = word[0]
if start_letter not in map:
map[start_letter] = [word]
else:
map[start_letter].append(word)
print(map)
for key in map.keys():
print('{}:{}'.format(key, map[key]))
运行程序,查看结果