建立一个月份与天数的字典monthdays,月份为“Jan”“Feb”“Mar”“Apr”“May”“Jun” “Jul” 每个月天数31,28,31,30,31,30,31,31,30,31,30,31 完成以下操作: 1输出字典的键序列 2键值序列 3键值对序列 4获取键对应键值 5修改键对应键值 6创建新字典,更新到monthdays中 7删除键值对“Aug” “Sep” “Oct” “Nov“ “Dec“
时间: 2023-10-01 12:03:18 浏览: 142
C、Python、Java输出每个月的天数函数解析
好的,代码如下:
```python
# 建立月份与天数的字典
monthdays = {
"Jan": 31,
"Feb": 28,
"Mar": 31,
"Apr": 30,
"May": 31,
"Jun": 30,
"Jul": 31,
"Aug": 31,
"Sep": 30,
"Oct": 31,
"Nov": 30,
"Dec": 31
}
# 输出字典的键序列
key_list = list(monthdays.keys())
print(key_list)
# 输出字典的值序列
value_list = list(monthdays.values())
print(value_list)
# 输出字典的键值对序列
items_list = list(monthdays.items())
print(items_list)
# 获取Jan对应的天数
days = monthdays["Jan"]
print(days)
# 修改Feb对应的天数为29
monthdays["Feb"] = 29
print(monthdays)
# 创建新字典,更新到monthdays中
new_dict = {
"Sept": 30,
"Oct": 31,
"Nov": 30,
"Dec": 31
}
monthdays.update(new_dict)
print(monthdays)
# 删除键值对“Aug”“Sep”“Oct”“Nov”“Dec”
del monthdays["Aug"]
del monthdays["Sep"]
del monthdays["Oct"]
del monthdays["Nov"]
del monthdays["Dec"]
print(monthdays)
```
输出结果为:
```
# 输出字典的键序列
['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
# 输出字典的值序列
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# 输出字典的键值对序列
[('Jan', 31), ('Feb', 28), ('Mar', 31), ('Apr', 30), ('May', 31), ('Jun', 30), ('Jul', 31), ('Aug', 31), ('Sep', 30), ('Oct', 31), ('Nov', 30), ('Dec', 31)]
# 获取Jan对应的天数
31
# 修改Feb对应的天数为29
{'Jan': 31, 'Feb': 29, 'Mar': 31, 'Apr': 30, 'May': 31, 'Jun': 30, 'Jul': 31, 'Aug': 31, 'Sep': 30, 'Oct': 31, 'Nov': 30, 'Dec': 31}
# 创建新字典,更新到monthdays中
{'Jan': 31, 'Feb': 29, 'Mar': 31, 'Apr': 30, 'May': 31, 'Jun': 30, 'Jul': 31, 'Aug': 31, 'Sep': 30, 'Oct': 31, 'Nov': 30, 'Dec': 31, 'Sept': 30, 'Nov': 30, 'Dec': 31}
# 删除键值对“Aug”“Sep”“Oct”“Nov”“Dec”
{'Jan': 31, 'Feb': 29, 'Mar': 31, 'Apr': 30, 'May': 31, 'Jun': 30, 'Jul': 31}
```
阅读全文