有时候我反问我自己,怎么不知道在Python 3中用更简单的方式做“这样”的事,当我寻求答案时,随着时间的推移,我当然发现更简洁、有效并且bug更少的代码。总的来说(不仅仅是这篇文章),“那些”事情总共数量是超过我想象的,但这里是第一批不明显的特性,后来我寻求到了更有效的/简单的/可维护的代码。
字典
字典中的keys()和items()
你能在字典的keys和items中做很多有意思的操作,它们类似于集合(set):
1 2 3 4 5 6 7 8 |
aa = {‘mike’: ‘male’, ‘kathy’: ‘female’, ‘steve’: ‘male’, ‘hillary’: ‘female’} bb = {‘mike’: ‘male’, ‘ben’: ‘male’, ‘hillary’: ‘female’} aa.keys() & bb.keys() # {‘mike’, ‘hillary’} # these are set-like aa.keys() - bb.keys() # {‘kathy’, ‘steve’} # If you want to get the common key-value pairs in the two dictionaries aa.items() & bb.items() # {(‘mike’, ‘male’), (‘hillary’, ‘female’)} |
太简洁啦!
在字典中校验一个key的存在
下面这段代码你写了多少遍了?
1 2 3 4 5 |
dictionary = {} for k, v in ls: if not k in dictionary: dictionary[k] = [] dictionary[k].append(v) |
这段代码其实没有那么糟糕,但是为什么你一直都需要用if语句呢?
1 2 3 4 |
from collections import defaultdict dictionary = defaultdict(list) # defaults to list for k, v in ls: dictionary[k].append(v) |
这样就更清晰了,没有一个多余而模糊的if语句。
用另一个字典来更新一个字典
1 2 3 4 5 6 7 |
from itertools import chain a = {‘x’: 1, ‘y’:2, ‘z’:3} b = {‘y’: 5, ‘s’: 10, ‘x’: 3, ‘z’: 6} # Update a with b c = dict(chain(a.items(), b.items())) c # {‘y’: 5, ‘s’: 10, ‘x’: 3, ‘z’: 6} |
这样看起来还不错,但是不够简明。看看我们是否能做得更好:
1 2 |
c = a.copy() c.update(b) |
更清晰而且更有可读性了!
从一个字典获得最大值
如果你想获取一个字典中的最大值,可能会像这样直接:
1 2 3 |
aa = {k: sum(range(k)) for k in range(10)} aa # {0: 0, 1: 0, 2: 1, 3: 3, 4: 6, 5: 10, 6: 15, 7: 21, 8: 28, 9: 36} max(aa.values()) #36 |
这么做是有效的,但是如果你需要key,那么你就需要在value的基础上再找到key。然而,我们可以用过zip来让展现更扁平化,并返回一个如下这样的key-value形式:
有时候我反问我自己,怎么不知道在Python 3中用更简单的方式做“这样”的事,当我寻求答案时,随着时间的推移,我当然发现更简洁、有效并且bug更少的代码。总的来说(不仅仅是这篇文章),“那些”事情总共数量是超过我想象的,但这里是第一批不明显的特性,后来我寻求到了更有效的/简单的/可维护的代码。
字典
字典中的keys()和items()
你能在字典的keys和items中做很多有意思的操作,它们类似于集合(set):
1 2 3 4 5 6 7 8 |
aa = {‘mike’: ‘male’, ‘kathy’: ‘female’, ‘steve’: ‘male’, ‘hillary’: ‘female’} bb = {‘mike’: ‘male’, ‘ben’: ‘male’, ‘hillary’: ‘female’} aa.keys() & bb.keys() # {‘mike’, ‘hillary’} # these are set-like aa.keys() - bb.keys() # {‘kathy’, ‘steve’} # If you want to get the common key-value pairs in the two dictionaries aa.items() & bb.items() # {(‘mike’, ‘male’), (‘hillary’, ‘female’)} |
太简洁啦!
在字典中校验一个key的存在
下面这段代码你写了多少遍了?
1 2 3 4 5 |
dictionary = {} for k, v in ls: if not k in dictionary: dictionary[k] = [] dictionary[k].append(v) |
这段代码其实没有那么糟糕,但是为什么你一直都需要用if语句呢?
1 2 3 4 |
from collections import defaultdict dictionary = defaultdict(list) # defaults to list for k, v in ls: dictionary[k].append(v) |
这样就更清晰了,没有一个多余而模糊的if语句。
用另一个字典来更新一个字典
1 2 3 4 5 6 7 |
from itertools import chain a = {‘x’: 1, ‘y’:2, ‘z’:3} b = {‘y’: 5, ‘s’: 10, ‘x’: 3, ‘z’: 6} # Update a with b c = dict(chain(a.items(), b.items())) c # {‘y’: 5, ‘s’: 10, ‘x’: 3, ‘z’: 6} |
这样看起来还不错,但是不够简明。看看我们是否能做得更好:
1 2 |
c = a.copy() c.update(b) |
更清晰而且更有可读性了!
从一个字典获得最大值
如果你想获取一个字典中的最大值,可能会像这样直接:
1 2 3 |
aa = {k: sum(range(k)) for k in range(10)} aa # {0: 0, 1: 0, 2: 1, 3: 3, 4: 6, 5: 10, 6: 15, 7: 21, 8: 28, 9: 36} max(aa.values()) #36 |
这么做是有效的,但是如果你需要key,那么你就需要在value的基础上再找到key。然而,我们可以用过zip来让展现更扁平化,并返回一个如下这样的key-value形式: