# 创建一个空字典
empty_dict = dict()
print(empty_dict) # 输出: {}
# 使用键值对创建字典
fruit_colors = dict(apple='red', banana='yellow')
print(fruit_colors) # 输出: {'apple': 'red', 'banana': 'yellow'}
# 从元组列表创建字典
tuple_list = [('apple', 'red'), ('banana', 'yellow')]
fruit_colors_from_tuples = dict(tuple_list)
print(fruit_colors_from_tuples) # 输出: {'apple': 'red', 'banana': 'yellow'}
# 从关键字参数创建字典
fruit_colors_from_kwargs = dict(apple='red', banana='yellow')
print(fruit_colors_from_kwargs) # 输出: {'apple': 'red', 'banana': 'yellow'}
# 从另一个字典创建字典
another_dict = {'apple': 'green', 'banana': 'yellow'}
new_dict = dict(another_dict)
print(new_dict) # 输出: {'apple': 'green', 'banana': 'yellow'}
上一篇:python 判断字符串是否为空
下一篇:python中try
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站