在Python中,有多種方法可以創(chuàng)建字典:
my_dict = {"key1": "value1", "key2": "value2"}
my_dict = dict(key1="value1", key2="value2")
my_dict = dict([("key1", "value1"), ("key2", "value2")])
keys = ["key1", "key2"]
default_value = 0
my_dict = dict.fromkeys(keys, default_value)
my_dict = {key: value for key, value in zip(keys, values)}
這些方法可以根據(jù)具體的需求和數(shù)據(jù)源選擇適合的方式創(chuàng)建字典。