Python字典

字典项目

字典项目是有序的、可更改的,并且不允许重复。

字典项以键:值对的形式呈现,并且可以使用键名来引用。

 

 info字典里面print出来是个列表,里面装着元组,元组可以用这种a,b=c的方法赋值,故有了下面这种第二种打印方法。

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict["brand"])

不允许重复

字典不能有两个具有相同键的项目:

重复值将覆盖现有值:

多变

字典是可变的,这意味着我们可以在字典创建后更改、添加或删除项目

字典长度

要确定字典有多少项,请使用以下 len()函数

字典项 - 数据类型

字典项中的值可以是任何数据类型:

访问项目

您可以通过引用方括号内的键名来访问字典的项目:

获取“模型”键的值:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
x = thisdict["model"]

 还有一个名为的方法get()会为您提供相同的结果:

x = thisdict.get("model")

获取key

keys()方法将返回字典中所有键的列表。

x = thisdict.keys()

键列表是字典的视图,这意味着对字典所做的任何更改都将反映在键列表中。

获取值

values()方法将返回字典中所有值的列表。

获取值列表:

x = thisdict.values()

获取items

items()方法将返回字典中的每个项目,作为列表中的元组。

获取键:值对的列表

x = thisdict.items()

添加键值队

向原始字典添加一个新项目,并看到键列表也得到更新:
x = car.keys()
print(x) #before the change

car["color"] = "white"
print(x) #after the change

检查密钥是否存在

要确定字典中是否存在指定的键,请使用in关键字:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
if "model" in thisdict:
  print("Yes, 'model' is one of the keys in the thisdict dictionary")

改变values

您可以通过引用其键名来更改特定项目的值:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict["year"] = 2018

更新字典

update()方法将使用给定参数中的项目更新字典。

参数必须是字典,或具有键值对的可迭代对象。

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.update({"year": 2020})

add也可以用update({"year": 2020})

移除项目

有几种方法可以从字典中删除项目:

pop()方法删除具有指定键名的项目:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.pop("model")
print(thisdict)

popitem()方法删除最后插入的项目(在 3.7 之前的版本中,会删除随机项目):

thisdict.popitem()

del关键字也可以完全删除字典

del thisdict

clear()方法清空字典:

thisdict.clear()

遍历字典

#一个一个地打印字典中的所有键名:
for x in thisdict:
  print(x)
#一个一个地打印字典中的所有值:
for x in thisdict:
  print(thisdict[x])
#您还可以使用该values()方法返回字典的值:
for x in thisdict.values():
  print(x)
#您可以使用该keys()方法返回字典的键:
for x in thisdict.keys():
  print(x)
#使用以下 方法循环遍历keys和valuesitems():
for x, y in thisdict.items():
  print(x, y)

复制字典

您不能简单地通过键入来复制字典dict2 = dict1,因为:dict2将只是对 的 引用dict1并且在 中所做的更改 dict1也会自动在 中进行 dict2

有很多方法可以制作副本,一种方法是使用内置的 Dictionary 方法 copy()

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
mydict = thisdict.copy()

制作副本的另一种方法是使用内置函数 dict()

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
mydict = dict(thisdict)

嵌套字典

字典可以包含字典,这称为嵌套字典。

例子

创建一个包含三个字典的字典:

myfamily = {
  "child1" : {
    "name" : "Emil",
    "year" : 2004
  },
  "child2" : {
    "name" : "Tobias",
    "year" : 2007
  },
  "child3" : {
    "name" : "Linus",
    "year" : 2011
  }
}

或者,如果您想将三个字典添加到新字典中:

child1 = {
  "name" : "Emil",
  "year" : 2004
}
child2 = {
  "name" : "Tobias",
  "year" : 2007
}
child3 = {
  "name" : "Linus",
  "year" : 2011
}

myfamily = {
  "child1" : child1,
  "child2" : child2,
  "child3" : child3
}

fromkeys()方法

创建一个包含 3 个键的字典,所有键的值都为 0:

x = ('key1', 'key2', 'key3')
y = 0
thisdict = dict.fromkeys(x, y)
print(thisdict)

fromkeys()方法返回具有指定键和指定值的字典。

dict.fromkeys(keys, value)

与上面的示例相同,但没有指定值:

x = ('key1', 'key2', 'key3')
thisdict = dict.fromkeys(x)
print(thisdict)

items()方法

car = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

x = car.items()
print(x)

返回:dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])

items()方法返回一个视图对象。视图对象包含字典的键值对,作为列表中的元组。
 

https://www.w3schools.com/python/python_dictionaries.asp

posted on 2022-03-28 09:45  -G  阅读(82)  评论(0)    收藏  举报

导航