Python: The Dictionary Playbook
来源:互联网 发布:fujixerox手机打印软件 编辑:程序博客网 时间:2024/06/10 04:12
Python: The Dictionary Playbook
I so often come across various kinds of boilerplate code regarding dictionaries in Python, that I decided to show some of it here and share the more concise way of performing the same operations. Presenting: The Dictionary Playbook.
1. The “Are You There?”
This one is pretty simple, but I’m amazed as to how it’s missed – finding out if a key exists in the dictionary.
The Lame Version
1
dct.has_key(key)
The Python Way
1
key in dct
2. The “Yoda Test”
For those programmers who master the “Are You There” play, there’s usually another simple, yet annoying behavior. It doesn’t only apply to dicts, but it’s very common.
Do This You Must Not
1
not key in dct
English, Do You Speak It?
1
key not in dct
3. The “Get the Value Anyway”
This one is really popular. You have a dictionary and a key, and you want to modify the key’s value. For example, adding 1 to it (let’s say you’re counting something).
The Boilerplate
123
if key not in dct: dct[key] = 0dct[key] = dct[key] + 1
The Awesome Way
1
dct[key] = dct.get(key, 0) + 1
dct.get(key[, default])
returns dct[key]
if it exists, and default
if not.
The Even More Awesome
If you’re using Python 2.7 and you want to count up amounts of stuff, you can use Counter.
1234
>>> from collections import Counter>>> d = [1, 1, 1, 2, 2, 3, 1, 1]>>> Counter(d)Counter({1: 5, 2: 2, 3: 1})
And here’s a more complete example:
123456789101112131415161718192021
>>> 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: 1Enter a number: 1Enter a number: 2Enter a number: 3Enter a number: 51Enter a number: 1Enter a number: 1Enter a number: 1Enter a number: 2Enter a number: 3You have entered 1, 5 times!You have entered 2, 2 times!You have entered 3, 2 times!You have entered 51, 1 times!
4. The “Make It Happen”
Sometimes your dictionary contains mutable objects, and you want to initialize and modify them. Let’s say you’re sorting out some data into a dictionary where the values are lists (examples courtesy of this answer in Stack Overflow).
Spelling It Out
123456
dct = {}for (key, value) in data: if key in dct: dct[key].append(value) else: dct[key] = [value]
Getting Down with the Python
1234
dct = {}for (key, value) in data: group = dct.setdefault(key, []) # key might exist already group.append(value)
What setdefault(key, default)
does is returns dct[key]
if it exists, and if it doesn’t – sets it to default
and returns it. Compared to get
, it’s useful when the default
value is an object you can modify, so you don’t have to manually reinsert its modified version to the dictionary.
Rocking it Out
123
dct = defaultdict(list)for (key, value) in data: dct[key].append(value) # all keys have a default already
defaultdict
is pretty awesome. It’s pretty self-explanatory – it’s a dict
with default values. This means that every access to a key in dct
that doesn’t exist in the dictionary (that would usually raise a KeyError
) creates it with the default value. It’s as if every access to dct
is done with setdefault
.
One interesting use I’ve found for defaultdict
is when implementing sparse data structures. You set defaultdict
to the default value and use coordinates (or whatever is applicable) as the key. I’ve used this to represents multi-dimensional grids and it’s definitely easier than using intricately wrapped lists.
An even more interesting example of its use is the one-line tree definition.
- Python: The Dictionary Playbook
- 字典的剧本(翻译自Python:The Dictionary Playbook)
- Dictionary access in Python the right way
- python Dictionary
- python Dictionary
- Python Dictionary
- Python Dictionary
- python dictionary
- python dictionary
- python---dictionary
- playbook
- return the index of a 'key' in Python dictionary
- Python调用Ansible 2.0 API执行playbook
- checkio-the flat dictionary
- The Flat Dictionary
- [The Hacker Playbook] 5.THE SCREEN-SOCIAL ENGINEERING
- [The Hacker Playbook] 7.THE QUARTERBACK SNEAK-EVADING AV
- The playbook to creeate the VMs from openstack cluster
- 使用Python进行稳定可靠的文件操作
- Linux系统管理员需要知道的16个服务器监控命令
- Python: Common Newbie Mistakes, Part 1
- Python: Common Newbie Mistakes, Part 2
- mono-3.2.3编译 VB.net Error : VBNC2017: The library 'Microsoft.VisualBasic.dll' could not be found.
- Python: The Dictionary Playbook
- Python程序的执行原理
- 搭建高可用mongodb集群(二)—— 副本
- 12 岁的孩子教你使用 Python 开发游戏
- Android中Bitmap,byte[],Drawable相互转化
- delphi中IDispath接口的实现类
- 动态捕获Python异常
- 加速你的Python代码
- Python的Lambda函数与排序