sentence = "a a a aa aa aa aaa aaa aaa aaa a aa a a aaa"
word_dict = {}
for word in sentence.split():
if word not in word_dict:
word_dict[word] = 0
word_dict[word] += 1
print('方法一')
print(word_dict)
word_dict1 = {}
for word in sentence.split():
word_dict1.setdefault(word, 0)
word_dict1[word] += 1
print('方法二')
print(word_dict1)
import collections as clt
word_dict2 = {}
word_dict2 = clt.defaultdict(int)
for word in sentence.split():
word_dict2[word] += 1
print('方法三')
print(word_dict2)
print('遍历字典')
print('方法一')
for key, value in word_dict.items():
print(key, value)
print('方法二')
for value, key in word_dict.items():
print(key, value)
tuple1 = (1, 1)
tuple2 = (1, (1, 1))
tuple3 = (1, (1, [1]))
word_dict[tuple1] = 55
word_dict[tuple2] = 56
print(word_dict.keys())
print(word_dict.values())
print(word_dict.items())
print('打印字典数据')
print(word_dict)
del word_dict[(1, 1)]
print('打印删除(1,1)之后的字典数据')
print(word_dict)