# 统计字符串中,各个字符的个数
a = input("输入一串英文:")
b = a.replace(' ', '')
# for keys in range(len(a)):
lib = {}
for key in b:
if key in lib.keys():
lib[key] += 1
else:
lib[key] = 1
print(lib)
# 籍贯统计(合并两个表)
dicGils = {'Jiangsu': 3, 'Zhejiang': 2, 'Jilin': 1}
dicBoys = {'Jiangsu': 8, 'Zhejiang': 5, 'Shandong': 5, 'Anhui': 4, 'Fujian': 2}
dicNew = dicBoys.copy()
for key in dicGils.keys():
if key in dicNew.keys():
dicNew[key] += 1
else:
dicNew[key] = 1
print(dicNew)
# 法二:(这里dicNew用的上面的结果)
for key, value in dicGils.items():
dicNew[key] = dicNew.get(key, 0) + value
print(dicNew)
# 将成绩按60,保存在字典的两个key值中
score = [11, 22, 33, 44, 55, 66, 77, 88, 99, 100]
dict = {'>=60': [], '<60': []}
for i in score:
if i >= 60:
dict['>=60'].append(i) # 对列表操作
else:
dict['<60'].append(i)
print(dict)
Python基础学习(字典篇)
于 2023-11-24 18:27:40 首次发布