编写一个 Python 程序以从字符串创建字典。
注意:跟踪字符串中字母的计数。
Python代码:
from collections import defaultdict, Counter
str1 = 'w3resource'
my_dict = {}
for letter in str1:
my_dict[letter] = my_dict.get(letter, 0)
print(my_dict)
def create_dictionary_from_string(input_string): 字符计数 = {}
for char in input_string:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
return char_count