Python-字典方法

时间:2020-02-23 14:42:38  来源:igfitidea点击:

在本教程中,我们将学习Python中的字典方法。

在上一个教程Python-字典中,我们了解了字典。
随时检查一下。

快速回顾

  • 字典是键值对的无序集合。

  • 我们使用大括号" {}"创建字典。

  • 使用冒号":"将键与值分开。

  • 字典的每个键值对均以逗号分隔。

好了,让我们开始使用字典方法。

  • clear
  • copy
  • fromkeys
  • get
  • items
  • keys
  • pop
  • popitem
  • setdefault
  • update
  • values

清除

我们使用clear方法从字典中删除所有项目。

在下面的Python程序中,我们将删除给定词典中的所有项目。

# dict
user = {
    "username": "theitroadtheitroad",
    "score": 9.1,
    "isOnline": False
}

user.clear()

复制

我们使用copy方法来复制现有字典。

在下面的Python程序中,我们将创建给定字典的副本。

# dict
user = {
    "username": "theitroadtheitroad",
    "score": 9.1,
    "isOnline": False
}

x = user.copy()

fromkeys

我们使用fromkeys方法创建具有给定键和值的字典。

在下面的Python程序中,我们使用传递给fromkeys方法的键来创建字典。

# keys
keys = ("a", "b", "c")

myDict = dict.fromkeys(keys)

print(myDict)

我们将获得以下输出。

{'a': None, 'b': None, 'c': None}

注意!如果未传递该值,则将" None"用作默认值。

在下面的Python程序中,我们使用传递给fromkeys方法的键创建字典,并将所有键设置为0。

# keys and value
keys = ("a", "b", "c")
value = 0

myDict = dict.fromkeys(keys, value)

print(myDict)     # {'a': 0, 'b': 0, 'c': 0}

get

我们使用get方法来获取给定字典的特定键的值。

在下面的Python程序中,我们得到"分数"键的值。

# dict
user = {
    "username": "theitroadtheitroad",
    "score": 9.1,
    "isOnline": False
}

print(user.get('score'))    # 9.1

注意!如果键在给定的字典中不存在,那么我们将得到"无"。

items

我们使用items方法获取一个列表,该列表包含由给定字典的键值对组成的元组。

在下面的Python程序中,我们使用items方法创建字典列表。

# dict
user = {
    "username": "theitroadtheitroad",
    "score": 9.1,
    "isOnline": False
}

items = user.items()

print(items)

我们将获得以下输出。

dict_items([('username', 'theitroadtheitroad'), ('score', 9.1), ('isOnline', False)])

keys

我们使用keys方法来获取给定字典的所有键。

在下面的Python程序中,我们将获取密钥。

# dict
user = {
    "username": "theitroadtheitroad",
    "score": 9.1,
    "isOnline": False
}

keys = user.keys()

print(keys)

我们将获得以下输出。

dict_keys(['username', 'score', 'isOnline'])

pop

我们使用pop方法使用给定键从字典中删除特定项(键/值对)。

在下面的Python程序中,我们将使用pop方法从字典中删除" isGameOver"项目。

# dict
user = {
    "username": "theitroadtheitroad",
    "score": 9.1,
    "isOnline": False,
    "isGameOver": False
}

item = user.pop('isGameOver')

print(item)    # False
print(user)    # {'username': 'theitroadtheitroad', 'score': 9.1, 'isOnline': False}

popitem

我们使用popitem从字典中删除最后插入的项目。

在下面的Python程序中,我们将插入一个新项,然后将其弹出。

# dict
user = {
    "username": "theitroadtheitroad",
    "score": 9.1,
    "isOnline": False
}

# insert item
user['isGameOver'] = False

print("before:", user)

# pop item
item = user.popitem()

print("popitem:", item)

print("after:", user)
before: {'username': 'theitroadtheitroad', 'score': 9.1, 'isOnline': False, 'isGameOver': False}
popitem: ('isGameOver', False)
after: {'username': 'theitroadtheitroad', 'score': 9.1, 'isOnline': False}

setdefault

我们使用setdefault方法来获取指定键的值。

如果字典中不存在该键,则插入该键,并将给定值传递给setdefault方法。

在下面的Python程序中,我们将获取字典中存在的"得分"键的值。

# dict
user = {
    "username": "theitroadtheitroad",
    "score": 9.1,
    "isOnline": False
}

item = user.setdefault('score', 9.2)

print(item)
print(user)
9.1
{'username': 'theitroadtheitroad', 'score': 9.1, 'isOnline': False}

在下面的Python程序中,键" isGameOver"被添加到字典中,因为它不存在。

# dict
user = {
    "username": "theitroadtheitroad",
    "score": 9.1,
    "isOnline": False
}

item = user.setdefault('isGameOver', False)

print(item)
print(user)
False
{'username': 'theitroadtheitroad', 'score': 9.1, 'isOnline': False, 'isGameOver': False}

update

我们使用update方法在字典中插入一个项目。

在下面的Python程序中,我们在给定的字典中插入一个新的项(键值对)isGameOver:False。

# dict
user = {
    "username": "theitroadtheitroad",
    "score": 9.1,
    "isOnline": False
}

user.update({'isGameOver': False})

print(user)
{'username': 'theitroadtheitroad', 'score': 9.1, 'isOnline': False, 'isGameOver': False}

我们使用"值"方法来获取字典的所有值。

在下面的Python程序中,我们将打印给定字典的所有值。

# dict
user = {
    "username": "theitroadtheitroad",
    "score": 9.1,
    "isOnline": False
}

values = user.values()

print(values)

我们将获得以下输出。

dict_values(['theitroadtheitroad', 9.1, False])