有哪些实用技巧能让Python字典操作更上一层楼?
- 内容介绍
- 文章标签
- 相关推荐
本文共计2681个文字,预计阅读时间需要11分钟。
关于Python字典,算得上是Python中相当重要的数据类型了。在你学会基础知识后,字典这个概念将伴随你后续的学习和工作。因此,这里有几个相对重要的知识点,大家务必掌握:
1. 字典是键值对集合,键是唯一的,值可以重复。
2.键必须是不可变类型,如字符串、数字、元组等。
3.值可以是任何类型的数据。
4.字典使用大括号{}表示,键值对之间用冒号:分隔,多个键值对用逗号,分隔。
5.可以使用方括号[]和键来访问字典中的值,如`dict[key]`。
6.可以使用`dict.keys()`、`dict.values()`和`dict.items()`方法分别获取字典的键、值和键值对列表。
7.字典支持迭代,可以遍历键、值或键值对。
8.可以使用`dict.get(key, default)`方法安全地获取字典中的值,如果键不存在,则返回默认值。
9.可以使用`dict.pop(key)`方法删除字典中的键值对,如果键不存在,则抛出异常。
10.字典支持多种操作,如合并、更新等。
字典这个概念,将会伴随着你后面的学习和工作。>>> # Python 2.7
>>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
>>> a_dict
{'color': 'blue', 'pet': 'dog', 'fruit': 'apple'}
>>> a_dict
{'color': 'blue', 'pet': 'dog', 'fruit': 'apple'}
>>> # Python 3.5
>>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
>>> a_dict
{'color': 'blue', 'pet': 'dog', 'fruit': 'apple'}
>>> a_dict
{'color': 'blue', 'pet': 'dog', 'fruit': 'apple'}
>>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
>>> a_dict
{'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
>>> a_dict
{'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}键值互换
>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4}
>>> new_dict = {}
>>> for key, value in a_dict.items():
... new_dict[value] = key
...
>>> new_dict
{1: 'one', 2: 'two', 3: 'thee', 4: 'four'}依据某种条件,过滤字典
>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4}
>>> new_dict = {} # Create a new empty dictionary
>>> for key, value in a_dict.items():
... if value <= 2:
... new_dict[key] = value
...
>>> new_dict
{'one': 1, 'two': 2}利用字典中的值,做一些计算
>>> incomes = {'apple': 5600.00, 'orange': 3500.00, 'banana': 5000.00}
>>> total_income = 0.00
>>> for value in incomes.values():
... total_income += value # Accumulate the values in total_income
...
>>> total_income
14100.0字典推导式
>>> objects = ['blue', 'apple', 'dog']
>>> categories = ['color', 'fruit', 'pet']
>>> a_dict = {key: value for key, value in zip(categories, objects)}
>>> a_dict
{'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}利用字典推导式,实现键值转换
>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} >>> new_dict = {value: key for key, value in a_dict.items()} >>> new_dict {1: 'one', 2: 'two', 3: 'thee', 4: 'four'}利用字典推导式,过滤字典
>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} >>> new_dict = {k: v for k, v in a_dict.items() if v <= 2} >>> new_dict {'one': 1, 'two': 2}利用字典推导式,做一些计算
>>> incomes = {'apple': 5600.00, 'orange': 3500.00, 'banana': 5000.00}
>>> total_income = sum([value for value in incomes.values()])
>>> total_income
14100.0字典排序
>> incomes = {'apple': 5600.00, 'orange': 3500.00, 'banana': 5000.00}
>>> sorted_income = {k: incomes[k] for k in sorted(incomes)}
>>> sorted_income
{'apple': 5600.0, 'banana': 5000.0, 'orange': 3500.0}内置函数,与字典配合使用
>>> prices = {'apple': 0.40, 'orange': 0.35, 'banana': 0.25}
>>> def discount(current_price):
... return (current_price[0], round(current_price[1] * 0.95, 2))
...
>>> new_prices = dict(map(discount, prices.items()))
>>> new_prices
{'apple': 0.38, 'orange': 0.33, 'banana': 0.24}filter()函数
>>> prices = {'apple': 0.40, 'orange': 0.35, 'banana': 0.25}
>>> def has_low_price(price):
... return prices[price] < 0.4
...
>>> low_price = list(filter(has_low_price, prices.keys()))
>>> low_price
['orange', 'banana']字典解包运算符
>>> vegetable_prices = {'pepper': 0.20, 'onion': 0.55}
>>> fruit_prices = {'apple': 0.40, 'orange': 0.35, 'pepper': .25}
>>> {**vegetable_prices, **fruit_prices}
{'pepper': 0.25, 'onion': 0.55, 'apple': 0.4, 'orange': 0.35}
本文共计2681个文字,预计阅读时间需要11分钟。
关于Python字典,算得上是Python中相当重要的数据类型了。在你学会基础知识后,字典这个概念将伴随你后续的学习和工作。因此,这里有几个相对重要的知识点,大家务必掌握:
1. 字典是键值对集合,键是唯一的,值可以重复。
2.键必须是不可变类型,如字符串、数字、元组等。
3.值可以是任何类型的数据。
4.字典使用大括号{}表示,键值对之间用冒号:分隔,多个键值对用逗号,分隔。
5.可以使用方括号[]和键来访问字典中的值,如`dict[key]`。
6.可以使用`dict.keys()`、`dict.values()`和`dict.items()`方法分别获取字典的键、值和键值对列表。
7.字典支持迭代,可以遍历键、值或键值对。
8.可以使用`dict.get(key, default)`方法安全地获取字典中的值,如果键不存在,则返回默认值。
9.可以使用`dict.pop(key)`方法删除字典中的键值对,如果键不存在,则抛出异常。
10.字典支持多种操作,如合并、更新等。
字典这个概念,将会伴随着你后面的学习和工作。>>> # Python 2.7
>>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
>>> a_dict
{'color': 'blue', 'pet': 'dog', 'fruit': 'apple'}
>>> a_dict
{'color': 'blue', 'pet': 'dog', 'fruit': 'apple'}
>>> # Python 3.5
>>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
>>> a_dict
{'color': 'blue', 'pet': 'dog', 'fruit': 'apple'}
>>> a_dict
{'color': 'blue', 'pet': 'dog', 'fruit': 'apple'}
>>> a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
>>> a_dict
{'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
>>> a_dict
{'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}键值互换
>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4}
>>> new_dict = {}
>>> for key, value in a_dict.items():
... new_dict[value] = key
...
>>> new_dict
{1: 'one', 2: 'two', 3: 'thee', 4: 'four'}依据某种条件,过滤字典
>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4}
>>> new_dict = {} # Create a new empty dictionary
>>> for key, value in a_dict.items():
... if value <= 2:
... new_dict[key] = value
...
>>> new_dict
{'one': 1, 'two': 2}利用字典中的值,做一些计算
>>> incomes = {'apple': 5600.00, 'orange': 3500.00, 'banana': 5000.00}
>>> total_income = 0.00
>>> for value in incomes.values():
... total_income += value # Accumulate the values in total_income
...
>>> total_income
14100.0字典推导式
>>> objects = ['blue', 'apple', 'dog']
>>> categories = ['color', 'fruit', 'pet']
>>> a_dict = {key: value for key, value in zip(categories, objects)}
>>> a_dict
{'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}利用字典推导式,实现键值转换
>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} >>> new_dict = {value: key for key, value in a_dict.items()} >>> new_dict {1: 'one', 2: 'two', 3: 'thee', 4: 'four'}利用字典推导式,过滤字典
>>> a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} >>> new_dict = {k: v for k, v in a_dict.items() if v <= 2} >>> new_dict {'one': 1, 'two': 2}利用字典推导式,做一些计算
>>> incomes = {'apple': 5600.00, 'orange': 3500.00, 'banana': 5000.00}
>>> total_income = sum([value for value in incomes.values()])
>>> total_income
14100.0字典排序
>> incomes = {'apple': 5600.00, 'orange': 3500.00, 'banana': 5000.00}
>>> sorted_income = {k: incomes[k] for k in sorted(incomes)}
>>> sorted_income
{'apple': 5600.0, 'banana': 5000.0, 'orange': 3500.0}内置函数,与字典配合使用
>>> prices = {'apple': 0.40, 'orange': 0.35, 'banana': 0.25}
>>> def discount(current_price):
... return (current_price[0], round(current_price[1] * 0.95, 2))
...
>>> new_prices = dict(map(discount, prices.items()))
>>> new_prices
{'apple': 0.38, 'orange': 0.33, 'banana': 0.24}filter()函数
>>> prices = {'apple': 0.40, 'orange': 0.35, 'banana': 0.25}
>>> def has_low_price(price):
... return prices[price] < 0.4
...
>>> low_price = list(filter(has_low_price, prices.keys()))
>>> low_price
['orange', 'banana']字典解包运算符
>>> vegetable_prices = {'pepper': 0.20, 'onion': 0.55}
>>> fruit_prices = {'apple': 0.40, 'orange': 0.35, 'pepper': .25}
>>> {**vegetable_prices, **fruit_prices}
{'pepper': 0.25, 'onion': 0.55, 'apple': 0.4, 'orange': 0.35}

