您的位置:首页 > 编程语言 > Python开发

字典的剧本(翻译自Python:The Dictionary Playbook)

2017-03-01 12:54 603 查看
原文http://blog.amir.rachum.com/post/39501813266/python-the-dictionary-playbook
翻译hit9
转自http://pyzh.readthedocs.io/en/latest/python-the-dictionary-playbook.html
我经常遇到各种五花八门的关于Python中的字典的代码用法,我决定在这个文章中展示一些并分享完成同样操作的更为简洁的做法。

2.1. 键的存在性(The “are you There?”)

不推荐的:

dct.has_key(key)


Python的做法:

key in dic


2.2. Yoda测试(The “Yoda Test”)

对于那些精通”Are Your There”的程序员,还有一个简单的同样也很恼人的行为,不仅仅寻于
dict
而言,它很普遍

如果你非要
Not


not key in dct #并不推荐


English,你会这么说?:

key not in dct


2.3. 无论如何时都要取到值(The “Get The Value Anyway”)

这个是相当流行的。你有一个
dict
key
,并且你想修改
key
对应的值,比如给它自增1(假如你在数数)

比较逊一点的做法:

if key not in dct:
def[key] = 0
dct[key] = dct[key] + 1


漂亮的做法:

dct[key] = dct.get(key, 0) + 1


关于
dct.get(key,[, default])
,如果
key
存在,返回
dct[key]
,否则返回
default


更棒的做法是

如果你在使用
Pthon 2.7
,你就可以用
Counter
来计数了:

>>> from collections import Counter
>>> d = [1, 1, 1, 2, 2, 3, 1, 1]
>>> Counter(d)
Counter({1: 5, 2: 2, 3: 1})


这里还有些更完整的例子:

>>> counter = Counter()
... for _ in range(10):
...     num = int(raw_input("Enter a number: "))
...     counter.update([num])
...
... for key, value in counter.iteritems():
...     print "You have entered {}, {} times!".format(key, value)
Enter a number: 1
Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 51
Enter a number: 1
Enter a number: 1
Enter a number: 1
Enter a number: 2
Enter a number: 3
You have entered 1, 5 times!
You have entered 2, 2 times!
You have entered 3, 2 times!
You have entered 51, 1 times!


2.4. 让它发生!(The “Make it Happen”)

有时你的字典里都是经常修改的对象,你需要初始化一些数据到这个字典,也需要修改其中的一些值。比如说,你在维护一个这样的
dict
它的值都是链表

写一个实现就是:

dct = {}
for (key, value) in data:
if key in dct:
dct[key].append(value)
else:
dct[key] = [value]


更Pythonic的做法是:

dct = {}
for (key, value) in data:
group = dct.setdefault(key, [])
group.append(value)


setdefault(key, default)
所做的是:如果存在,返回
dct[key]
,不存在则把
dct[key]
设为
default
并返回它。当一个默认的值是一个你可以修改的对象的时候这是很有用的。

使用
defaultdict


dct = defaultdict(list)
for (key, value) in data:
dct[key].append(value)


defaultdict
非常棒,它每生成一对新的
key-value
,就会给
value
一个默认值,这个默认值是
defaultdict
的参数。(注:
defaultdict
在模块
collections
中)

一个很有意思的就是,
defaultdict
实现的一行的tree:

https://gist.github.com/2012250
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: