基本用法
# 创建字典
dict = {'Maths':99, 'Chinese':90, 'English':65}
print(dict) # {'Chinese': 90, 'English': 65, 'Maths': 99}
# 增
dict['Physics'] = 100
print(dict) # {'Chinese': 90, 'Physics': 100, 'English': 65, 'Maths': 99}
# 改
dict['English'] = 95
print(dict) # {'Chinese': 90, 'Physics': 100, 'English': 95, 'Maths': 99}
# 删
del dict['Maths']
print(dict) # {'Chinese': 90, 'Physics': 100, 'English': 95}
有序字典
使用OrderedDict创建有序字典。
import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'
dic['k2'] = 'v2'
dic['k3'] = 'v3'
print(dic) # OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])
# 清空有序列表
dic.clear()
print(dic) # OrderedDict()
使用函数popitem()按照后进先出原则,删除最后加入的元素并返回键值对。
import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'
dic['k2'] = 'v2'
dic['k3'] = 'v3'
print(dic.popitem(), dic) # ('k3', 'v3') OrderedDict([('k1', 'v1'), ('k2', 'v2')])
print(dic.popitem(), dic) # ('k2', 'v2') OrderedDict([('k1', 'v1')])
获取字典最值
函数zip()可以将可迭代对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。利用*操作符,可以将元组解压为列表。
对字典进行比较运算时,通常需要把字典的键和值反转过来。
price ={
'A' : 1899,
'B' : 999,
'C' : 3999,
'D' : 2999,
'E' : 5000
}
print(price.keys()) # dict_keys(['C', 'B', 'E', 'D', 'A'])
print(price.values()) # dict_values([3999, 999, 5000, 2999, 1899])
print(zip(price.values(), price.keys())) # <zip object at 0x00000181D2901388>
print(*zip(price.values(), price.keys())) # (3999, 'C') (999, 'B') (5000, 'E') (2999, 'D') (1899, 'A')
min_price = min(zip(price.values(), price.keys()))
print(min_price) # (999, 'B')
max_price = max(zip(price.values(), price.keys()))
print(max_price) # (5000, 'E')
sorted_price = sorted(zip(price.values(), price.keys()))
print(sorted_price) # [(999, 'B'), (1899, 'A'), (2999, 'D'), (3999, 'C'), (5000, 'E')]
print(min(price)) # A
print(max(price)) # E
print(min(price.values())) # 999
print(max(price.values())) # 5000
print(min(price, key=lambda k: price[k])) # B
print(max(price, key=lambda k: price[k])) # E
获取字典中相同键值对
- 函数 keys()
函数keys()能返回keys-view对象,,支持常见的集合操作,如求并集、交集和差集。
- 函数 items()
函数items()能返回由键值对组成的items-view对象,支持类似的集合操作。
a = {
'x' : 1,
'y' : 2,
'z' : 3
}
b = {
'x' : 11,
'y' : 2,
'z' : 10
}
print(a.keys() & b.keys()) # {'x', 'y', 'z'}
print(a.keys() - b.keys()) # set()
print(a.items() & b.items()) # {('y', 2)}
c = {key: a[key] for key in a.keys() - {'z', 'w'}}
print(c) # {'x': 1, 'y': 2}
对字典进行排序
函数 itemgetter()的功能是获取对象中指定域的值,参数为一些序号(需要获取的数据在对象中的序号)。
from operator import itemgetter
a = [1, 2, 3]
b = {'a':1, 'b':2, 'c':3}
i = itemgetter(1)
print(i(a)) # 2
i = itemgetter('c')
print(i(b)) # 3
i = itemgetter('b', 'c')
print(i(b)) # (2, 3)
from operator import itemgetter
rows = [
{'name': 'CCC', 'id': 1003},
{'name' : 'BBB', 'id' : 1004},
{'name': 'EEE', 'id': 1001},
{'name' : 'DDD', 'id' : 1002},
{'name': 'AAA', 'id': 1005}
]
rows_by_name = sorted(rows, key=itemgetter('name'))
print(rows_by_name)
# [{'name': 'AAA', 'id': 1005}, {'name': 'BBB', 'id': 1004}, {'name': 'CCC', 'id': 1003}, {'name': 'DDD', 'id': 1002}, {'name': 'EEE', 'id': 1001}]
rows_by_id = sorted(rows, key=itemgetter('id'))
print(rows_by_id)
# [{'name': 'EEE', 'id': 1001}, {'name': 'DDD', 'id': 1002}, {'name': 'CCC', 'id': 1003}, {'name': 'BBB', 'id': 1004}, {'name': 'AAA', 'id': 1005}]
print(min(rows, key=itemgetter('id'))) # {'id': 1001, 'name': 'EEE'}
print(max(rows, key=itemgetter('id'))) # {'id': 1005, 'name': 'AAA'}







网友评论