TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Python: The Dictionary Playbook

193 点作者 Nurdok超过 12 年前

21 条评论

elbear超过 12 年前
You mentioned it a bit, but I want to make it clear that, even if you don't use 2.7, you can still count by doing:<p><pre><code> from collections import defaultdict counter = defaultdict(int) </code></pre> There is a difference though, because you have to count manually, i.e:<p><pre><code> for i in 'supercalifragilisticexpialidocius': counter[i] += 1 </code></pre> Also, because defaultdict accepts any callable, you can have a dict of counters by doing:<p><pre><code> counters = defaultdict(lambda: defaultdict(int)) for word in ['apple', 'berry', 'grape']: for letter in word: counters[word][letter] += 1 </code></pre> This is not very obvious, so I don't use it a lot, but sometimes it's the most elegant solution.
评论 #4999867 未加载
评论 #5000826 未加载
avolcano超过 12 年前
You know a guide is good when it makes you want to go back and refactor old code. Great information, thanks :)
评论 #5000754 未加载
评论 #5001503 未加载
aidos超过 12 年前
This is a really nice guide. I've been up to my eyes in python dicts over the last few days so already had most of these figured out.<p>setdefault is new to me, which is cool. Unfortunately I can only see one place to use it in my code and it would be inefficient [0]. Best stash it away for later use :)<p>[0] r = re_subs.setdefault(s, re.compile(s))
评论 #5000189 未加载
simon_weber超过 12 年前
Good advice. I've got one to add:<p><pre><code> #x and y are dictionaries z = dict(x.items() + y.items()) </code></pre> It merges two dictionaries, giving precedence to the second (in Python 2 - Python 3 is a bit more nasty: <a href="http://stackoverflow.com/questions/38987/how-can-i-merge-union-two-python-dictionaries-in-a-single-expression" rel="nofollow">http://stackoverflow.com/questions/38987/how-can-i-merge-uni...</a>).
评论 #5001183 未加载
textminer超过 12 年前
Love defaultdict. It and dict/set/list compressions are a big part of what makes Python so fast to write in.<p>Great practice for 2.7 that's probably quashed in 3.0. For large dicts, no need to create a giant set en route when iterating over keys, values, or both. Use "for k in d.iterkeys()", "for v in d.itervals", "for k,v in d.iteritems."<p>While I'm at it-- if you're ever finding yourself using a huge amount of awfully rigid objects from a single class, use __slots__ to allocate needed variables! Python will otherwise define the object's namespace in a dict (called __dict__) which allocates a whole kilobyte per object. Bad news if you have several hundred thousand... Guessing this is why Guido loves namedtuples so much for basic attributed storage.
评论 #5001090 未加载
pcote超过 12 年前
I love curiosity sparking posts like this.<p>&#62;&#62;key in dct<p>is much better than<p>key in dct.keys()<p>Of course, that got me curios to find out if there is a magic method out there that takes advantage of keyword "in". Turns out __contains__ does that.<p>Always exciting to stumble upon new stuff in my favorite language.
hartror超过 12 年前
I hate to be that guy but what about dictionary comprehensions?
评论 #5000318 未加载
Goopplesoft超过 12 年前
One my favorites, safe deep searching: by returning a dict you can run another .get<p>var = {'a' : 'b' , 'c' : {'d' : 'f'}}<p>print var.get('c', {}).get('d') print var.get('DNE', {}).get('d')
solox3超过 12 年前
"not key in dct", or "not (key in dct)", is never slower than "key not in dct" because when key is found in dct, the expression is immediately true.
评论 #5001403 未加载
评论 #5000315 未加载
评论 #5001222 未加载
taejo超过 12 年前
In item 3, the "boilerplate" and the "awesome way" are not equivalent. The boilerplate does the equivalent of setdefault, which is mentioned later<p><pre><code> dct[key] = dct.setdefault(key, 0) + 1</code></pre>
评论 #5002031 未加载
评论 #5002018 未加载
stillinbeta超过 12 年前
In most circumstances for (key, val) in data won't work, as the default dictionary iterable only contains the keys. You want for key, val in data.items():
评论 #5000862 未加载
评论 #5000814 未加载
评论 #5001423 未加载
danielwozniak超过 12 年前
Nice! Some good information here. Good for beginners, good for intermediates, some expert will probably say it's good for them too.
goronbjorn超过 12 年前
Thanks for writing this; it's very useful.<p>Are you planning on doing similar posts about other parts of Python in the future?
mixedbit超过 12 年前
Can logical operator in Python be used to conditionally initialize an item in a dict? For example, can:<p><pre><code> group = dct.setdefault(key, []) group.append(value) </code></pre> be replaced with some equivalent of this Ruby snippet:<p><pre><code> (dict[key] ||= []) &#60;&#60; value ?</code></pre>
评论 #5001864 未加载
评论 #5001817 未加载
simgidacav超过 12 年前
Thanks, I didn't know about "x not in D"! On the same note:<p><a href="http://dacavtricks.wordpress.com/2011/05/23/python-default-values-in-a-dictionary/" rel="nofollow">http://dacavtricks.wordpress.com/2011/05/23/python-default-v...</a>
bismark超过 12 年前
I also find the combination of lambda and defaultdict quite useful:<p><pre><code> d = defaultdict(lambda: False) </code></pre> or<p><pre><code> d = defaultdict(lambda: {'foo':set(), 'bar':False}) d['baz']['foo'].add(1)</code></pre>
评论 #5001587 未加载
kriro超过 12 年前
Pretty cool, bookmarked. I always enjoy different forms of presenting the info, playbook was a nice touch :)
manojarcom超过 12 年前
my Bitdefender is blocking the site; says it's insecure.
eagle9超过 12 年前
nice! very useful, thanks for putting this together.
nu2ycombinator超过 12 年前
This is ... wait for it.. Legendary. :)
blt超过 12 年前
Rocking it out! Awesome! Getting down! Make it happen!