你在这里是因为你想学习如何在 Python 中初始化一个字典。而我将在这里向你展示如何做。
什么是 python 中的 dictionary ?
在 Python 中,Dictionary 是一个无序的项目集合,包含成对的键和值。我们可以通过下面的例子更好地理解。
dictionary = { 1:"integer", 2.03:"Decimal", "Lion":"Animal"}
在上面的字典中:
- "integer "是键 "1 "的一个值
- "十进制 "是键值 "2.03 "的值
- "动物 "是键值 "Lion "的值。
初始化 Python 字典的不同方法
我们可以在 python 中使用以下不同的方法来定义或初始化我们的 dictionary。
通过传递字数初始化字典
我们可以通过传递键值对作为字面符号来创建一个 dictionary。下面是一个适当的例子,传递字词的语法如下:
dictionary_name = {
key1 : value1,
key2 : value2,
key3 : value3
}
my_dictionary = {
"Lion" : "Animal",
"Parrot" : "Bird",
"Cobra" : "Reptile",
"Human" : "Mammals"
}
print(my_dictionary)
我们的字典名为 "my_dictionary
" ,将像这样创建:
{'Lion': 'Animal', 'Parrot': 'Bird', 'Cobra': 'Reptile', 'Human': 'Mammals'}
通过传递字词来初始化字典
使用 dict() 构造函数初始化字典
我们可以使用dict
() 构造函数创建一个字典,如下所示:
my_dictionary = dict(
Lion = "Animal",
Parrot = "Bird",
Cobra = "Reptile",
Human = 'Mammals'
)
print(my_dictionary)
我们的字典将像前面的方法一样被创建。
{'Lion': 'Animal', 'Parrot': 'Bird', 'Cobra': 'Reptile', 'Human': ' Mammals'}
使用Dict
() 构造函数初始化字典
使用列表初始化字典
我们可以通过使用两个不同的键和值的列表来创建字典,如下所示。
让我们通过下面的代码片断分别创建两个不同的键和值的列表:
Keys = ["Lion", "Parrot", "Cobra", "Human"]
Values = ["Animal", "Bird", "Reptile", "Mammals"]
我们将在dict
() 构造函数中使用zip
() 方法,如下所示:
my_dict = dict(zip(Keys,Values ))
print(my_dict)
我们的字典将被创建如下:
{1: 'Integer', 2.0: 'Decimal', 'Lion': 'Animal', 'Parrot': 'Bird'}
使用初始化字典Lists
使用图元初始化字典
我们可以使用图元来创建一个字典。一个元组是一个单一变量中多个项目的集合,与数据类型无关。它可以存储一个或多个不同数据类型的值。我们可以通过使用图元来创建一个字典。让我们首先创建一个图元的列表。
Tuples = [(1 , "Integer"), (2.0 , "Decimal"), ("Lion" , "Animal"),("Parrot" , "Bird")]
My_dict = dict(Tuples)
print(My_dict)
在上面的代码片段中,(1 , "Integer"), (2.0 , "Decimal"), ("Lion" , "Animal")
和("Parrot" , "Bird")
是图元。
我们已经创建了一个名为 "Tuples
"的图元列表。我们通过在dict
() 构造函数中传递 "Tuple
" 作为参数来创建我们需要的Dictionary
。我们的字典被创建如下。
{1: 'Integer', 2.0: 'Decimal', 'Lion': 'Animal', 'Parrot': 'Bird'}
使用图元初始化字典
使用__setitem__
方法初始化 dictionary
我们可以按照下面给出的代码片断,用__setitem__
方法创建一个字典。首先,我们需要创建一个空的 dictionary。
dict2 = {}
我们需要在dict2 中输入键和值 ,使用 **__setitem__
**方法输入到 dict2 中,如下所示:
dict2.__setitem__('key1', 'GEEK')
dict2.__setitem__('key2', 'God')
dict2.__setitem__('key3', 'is')
dict2.__setitem__('key4', 'Great')
print(dict2)
我们的字典将像这样创建:
{'key1': 'GEEK', 'key2': 'God', 'key3': 'is', 'key4': 'Great'}
使用__setitem__
方法初始化字典
使用fromkeys
()方法初始化字典
我们可以使用 fromkeys() 方法为字典中的某些键分配一个普通的值。
让我们取一个键的列表,使用fromkeys
() 方法,如下所示:
new_key = ["James", "Potter", "mathew"]
dict3 = dict.fromkeys(new_key, "men")
print(dict3)
print 函数应该打印上述代码片断的字典:
{'James': 'men', 'Potter': 'men', 'mathew': 'men'}
使用fromkeys
() 方法初始化字典
而不是使用fromkeys
() 方法,我们也可以应用一个for 循环来获得与上面相同的输出:
dict4={}
for x in new_key:
dict4[x] = "Men"
我们可以得到同样的字典:
{'James': 'men', 'Potter': 'men', 'mathew': 'men'}
使用For循环而不是fromkeys
()方法来初始化字典
使用setdefault
() 方法初始化字典
我们可以使用 setdefault() 方法来初始化 python 中的 dictionary,如下所示。这个方法返回列表中的一个键的值。
让我们创建一个名为my_dictionary
的空字典,并使用setdefault
() 方法将空值作为各自的键值。
my_dictionary = {}
[my_dictionary.setdefault(i, []) for i in range(4)]
同样,在我们的字典中追加各自需要的值:
my_dictionary[0].append('George')
my_dictionary[1].append('John')
print(my_dictionary)
我们可以得到我们的字典,如下所示:
{0: ['George'], 1: ['John'], 2: [], 3: []}
再一次,追加一个值:
my_dictionary[3].append('hrithik')
print(my_dictionary)
再一次,我们可以得到我们的字典,如下所示:
{0: ['George'], 1: ['John'], 2: [], 3: ['hrithik'] }
使用setdefault
() 方法初始化字典
初始化一个空的 dictionary
我们可以通过下面的代码片断来初始化一个空的 dictionary:
My_dict = {}
它将创建一个空的 dictionary:
print(My_dict)
{}
空的字典
总结
在这篇文章中,我们介绍了如何以不同的方式初始化一个 Python 字典。希望你一定已经练习并喜欢上了我们的代码片断。我们必须再次访问一些更令人兴奋的话题。