创建字典dict1={'k1:'V1','K2:','V2','K3':[11,22,33]} (1)访问K2对应的值V2(用两种方法) 字典 (2)诗在字典中添加一个键值对.“K4”、“V4”,输出添加后的字典 (3)请在修改字典中“K1”对应的值为“Kidd”,输出修改后的字典 14)请循环输出所有的value. 15)请循环输出所有的key. 16)请循环输出所有的value和key (7)在K3对应的值的未尾追加一个完素44,输出修改后的字典 18)在K3对应的值的第1个位置插入元素18,输出修改后的字典 19)删除K2元素,并输出删除元素之后的字典 (10)清空整个字典,并输出修改后的字典
时间: 2023-06-25 14:05:11 浏览: 452
dotty_dict:字典包装器,用于快速访问深层嵌套的键
1)
```python
dict1 = {'k1': 'V1', 'K2': 'V2', 'K3': [11, 22, 33]}
# 方法1
print(dict1['K2'])
# 方法2
print(dict1.get('K2'))
```
输出结果:
```
V2
V2
```
2)
```python
dict1['K4'] = 'V4'
print(dict1)
```
输出结果:
```
{'k1': 'V1', 'K2': 'V2', 'K3': [11, 22, 33], 'K4': 'V4'}
```
3)
```python
dict1['k1'] = 'Kidd'
print(dict1)
```
输出结果:
```
{'k1': 'Kidd', 'K2': 'V2', 'K3': [11, 22, 33], 'K4': 'V4'}
```
4)
```python
for value in dict1.values():
print(value)
```
输出结果:
```
Kidd
V2
[11, 22, 33]
V4
```
5)
```python
for key in dict1.keys():
print(key)
```
输出结果:
```
k1
K2
K3
K4
```
6)
```python
for key, value in dict1.items():
print(key, value)
```
输出结果:
```
k1 Kidd
K2 V2
K3 [11, 22, 33]
K4 V4
```
7)
```python
dict1['K3'].append(44)
print(dict1)
```
输出结果:
```
{'k1': 'Kidd', 'K2': 'V2', 'K3': [11, 22, 33, 44], 'K4': 'V4'}
```
8)
```python
dict1['K3'].insert(0, 18)
print(dict1)
```
输出结果:
```
{'k1': 'Kidd', 'K2': 'V2', 'K3': [18, 11, 22, 33, 44], 'K4': 'V4'}
```
9)
```python
del dict1['K2']
print(dict1)
```
输出结果:
```
{'k1': 'Kidd', 'K3': [18, 11, 22, 33, 44], 'K4': 'V4'}
```
10)
```python
dict1.clear()
print(dict1)
```
输出结果:
```
{}
```
阅读全文