# 创建一个字典 operators_dict
operators_dict = {'<': 'less than','==': 'equal'}
# 先打印一行
print('Here is the original dict:')
# 在使用 for 循环 遍历 使用 sorted 函数 排序 包含 operators_dict 所有键值对的列表
for key, value in sorted(operators_dict.items()):
# 输出类似字符串
print(f'Operator {key} means {value}.')
# 增加键值对
operators_dict['>'] = 'greater than'
# 输出一个换行
print()
# 在打印一行字符串
print('The dict was changed to:')
# 再来个和上述一样的for循环
for key, value in sorted(operators_dict.items()):
print(f'Operator {key} means {value}.')
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | d = {'<':'less than', '==':'equal'} print('Here is the original dict:') forx,y in sorted(d.items()): print(f'Operator {x} means {y}.') d.setdefault('>', 'greater than') print() print('The dict was changed to:') forx,y in sorted(d.items()): print(f'Operator {x} means {y}.') # print('Operator < means less than.') # d.setdefault('>', 'greater than') # print(d) # n = sorted(d) # fori in n: # print(i) # print('The dict was changed to:') |
operators_dict={'<':'less than','==':'equal'}
print('Here is the original dict:')
for k,v in sorted(operators_dict.items()):
print(f'Operators {k} means {v}')
operators_dict['>']='greater than'
print()
print('The dict was changed to:')
for k,v in sorted(operators_dict.items()):
print(f'Operators {k} means {v}') operators_dict = {'<': 'less than', '==': 'equal'}
print('Here is the original dict:')
sorted(operators_dict)
for x, y in operators_dict.items():
print(f'Operator {x} means {y}.')
operators_dict['>'] = 'greater than'
print()
# 输出一个换行!!!print('\n')输出两个换行
print('The dict was changed to:')
sorted(operators_dict)
for x, y in operators_dict.items():
print(f'Operator {x} means {y}.') dict={'<':'less than','==':'equal'}
print('Here is the original dict:')
for i in sorted(dict):
print('Operator %s means %s.'%(i,dict[i]))
dict['>']='greater than'
print('\nThe dict was changed to:')
for i in sorted(dict):
print('Operator %s means %s.'%(i,dict[i])) operators_dict = {'<': 'less than' , '==': 'equal'}
print('Here is the original dict:')
# 对字典key升序排序--
my_list = sorted(operators_dict)
for i in my_list:
value = operators_dict[i]
print(f'Operator {i} means {value}.')
# 这里要输出一个空行,易错
print('')
# 字典新增元素---字典名[key]=value。就表示新增,因为字典的key是唯一的
operators_dict['>'] = 'greater than'
print('The dict was changed to:')
# 再次遍历列表
my_list_new = sorted(operators_dict)
for i in my_list_new:
value = operators_dict[i]
print('Operator {} means {}.'.format(i,value)) operators_dict={'<': 'less than','==': 'equal'}
print('Here is the original dict:')
l1=sorted(operators_dict)
for i in l1:
print('Operator '+i+' means '+operators_dict[i]+'.')
operators_dict['>']='greater than'
print(sep='/')
print('The dict was changed to:')
l2=sorted(operators_dict)
for i in l2:
print('Operator '+i+' means '+operators_dict[i]+'.')
感觉题目好像是要求只遍历键(key)
operators_dict = {'<': 'less than','==': 'equal'}
print('Here is the original dict:')
for i in sorted(operators_dict):
print(f'Operator {i} means {operators_dict[i]}.')
operators_dict['>'] = 'greater than'
print()
print('The dict was changed to:')
for i in sorted(operators_dict):
print(f'Operator {i} means {operators_dict[i]}.')