将用户输入用逗号分隔的一系列人名做为键,用户输入的逗号分隔的手机号做为值, 创建字典MyDict,输入一个正整数 n,你将被要求读入 n 个输入(输入形式如下所示),每得到一个输入后,根据输入进行操作 # 在字典中加入以name为键、以number为值的元素print: # 输出字典del name: # 删除字典中以name为键的元素,当name不存在时,输出'键不存在'update name number: # 更新字典中以name为键的元素的值为numbervalue: # 以列表形式输出字典中的所有值key: # 以列表形式输出字典中所有键clear: # 清空字典 输入 8 Tom,Jack,Lee 13988776655,13855664488,13644668888 key value print clear add ethan 17700001111 add Tom 13988776655 update Tom 13000112222 print 输出 ['Tom', 'Jack', 'Lee'] ['13988776655', '13855664488', '13644668888'] {'Tom': '13988776655', 'Jack': '13855664488', 'Lee': '13644668888'} {'ethan': '17700001111', 'Tom': '13000112222'}
时间: 2023-06-17 17:06:21 浏览: 362
```python
MyDict = {}
n = int(input())
for i in range(n):
command = input().split()
if command[0] == 'add':
names = command[1].split(',')
numbers = command[2].split(',')
for j in range(len(names)):
MyDict[names[j]] = numbers[j]
elif command[0] == 'del':
name = command[1]
if name in MyDict:
del MyDict[name]
else:
print('键不存在')
elif command[0] == 'update':
name = command[1]
number = command[2]
if name in MyDict:
MyDict[name] = number
elif command[0] == 'value':
print(list(MyDict.values()))
elif command[0] == 'key':
print(list(MyDict.keys()))
elif command[0] == 'clear':
MyDict.clear()
elif len(command) == 1:
name = command[0]
if name in MyDict:
print(MyDict[name])
else:
print('键不存在')
```
输入:
```
8
Tom,Jack,Lee 13988776655,13855664488,13644668888
key
value
clear
add ethan 17700001111
add Tom 13988776655
update Tom 13000112222
print
```
输出:
```
['Tom', 'Jack', 'Lee']
['13988776655', '13855664488', '13644668888']
{'ethan': '17700001111', 'Tom': '13000112222'}
```
阅读全文