字典的剧本(翻译自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 |
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
相关文章推荐
- Python: The Dictionary Playbook
- Python 字典(Dictionary)
- return the index of a 'key' in Python dictionary
- Python 字典(Dictionary) setdefault()方法
- python字典(dictionary)中get和items、iteritems方法
- C# 字典(dictionary)练习,日期翻译
- dictionary字典的初步介绍(一) python
- 【代码学习】PYTHON字典(Dictionary)
- 1006- Python 字典(Dictionary) items()方法
- Python3的字典dictionary和set初学总结(未完)
- Python字典(Dictionary)
- Python探索记(13)——字典Dictionary
- Python 字典(Dictionary) get()方法
- Python 字典(Dictionary)
- Python学习笔记(十八)----Python 字典(Dictionary)
- Python-Dictionary-创建字典的方式
- Python中的字典(Dictionary)
- Python基础--字典(dictionary)
- Python 字典(Dictionary)操作详解
- 【Python】Learn Python the hard way, ex39 字典操作