TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A Hodgepodge of Python

75 pointsby donmccover 13 years ago

9 comments

lysiumover 13 years ago
Didn't know the timeit module. However, the presented usage to time list reversing is ill: The timing is dominated by the list creation, which should go into setup. Then I'll get:<p><pre><code> $ python -mtimeit -s'l=range(1000)' 'l[::-1]' 100000 loops, best of 3: 3.05 usec per loop $ python -mtimeit -s'l=range(1000)' 'l.reverse()' 1000000 loops, best of 3: 0.535 usec per loop $ python -mtimeit -s'l=range(1000)' 'a=reversed(l)' 1000000 loops, best of 3: 0.39 usec per loop </code></pre> So, instead of reversing the list in place, creating a new, reversed list seems to be faster.
评论 #3002235 未加载
balbeitover 13 years ago
I always love learning little tidbits about Python like these. Thanks!<p>Another I found quite useful: instead of collections.defaultdict, you can use the dict's get() method to set a default value if the key doesn't exist. get() takes the key and a default value, and if the key doesn't exist it creates one with the default value provided.<p><pre><code> a = {} a['foo'] = a.get('foo',0) + 1 # a = {'foo':1} a['foo'] = a.get('foo',0) + 1 # a = {'foo':2} </code></pre> It can be very useful for incrementing keys in a dict, even if they did not exist previously.
评论 #3002714 未加载
评论 #3002712 未加载
评论 #3005028 未加载
BrandonMover 13 years ago
I use defaultdict a lot. It's useful not only for counters, but also for sets, lists, dicts, and even other defaultdicts. It usually results in very succinct code compared to alternatives:<p><pre><code> occurrences = defaultdict(int) digraphs = defaultdict(lambda: defaultdict(int)) prev_ch = None for ch in long_text: occurrences[ch] += 1 if prev_ch is not None: digraphs[prev_ch][ch] += 1 prev_ch = ch print "Occurrences:" for ch, count in sorted(occurrences.iteritems()): print " %s: %i" % (ch, count) print "Digraph occurences:" for ch1, counts in sorted(digraphs): for ch2, count in sorted(counts): print " %s%s: %i" % (ch1, ch2, count) </code></pre> (I'm aware that digraphs could be done as a single flat dict; it's just a trivial example to show defaultdict nesting.)
notaddictedover 13 years ago
I'm still on the fence as to whether this is <i>acceptably ugly</i> or <i>unacceptably ugly</i>, but it does make use of a python feature:<p><pre><code> &#62;&#62;&#62; &#62;&#62;&#62; class PrefixStr: ... def __init__(self, prefix): ... self.prefix = prefix ... def __getattr__(self, s): ... return self.prefix+s ... &#62;&#62;&#62; &#62;&#62;&#62; q = PrefixStr('xyz_') &#62;&#62;&#62; &#62;&#62;&#62; { q.height:11, q.width:7, q.depth:3 } {'xyz_width': 7, 'xyz_depth': 3, 'xyz_height': 11} &#62;&#62;&#62;</code></pre>
评论 #3002523 未加载
评论 #3003255 未加载
gbogover 13 years ago
The more I use Python the more I like it. It allows a lot of the early refactorings needed for a proof of concept prototype. Playing with it feels like playing with a well lubrificated rubix cube, it rotates well in all directions, allowing quick changes in the code's structure. I can't tell exactly why but I think the use of "self" in object methods, while a bit weird in the begining, is one of the best part. Eg morphing a helper function into a method is just a matter of one search and replace.
ashrustover 13 years ago
This is the stuff that brings me back to hacker news everyday. So helpful.
mumrahover 13 years ago
<i>izip</i> is the super useful generator counterpart of <i>zip</i><p>Also, 2.7 introduced a couple of cool things in collections: <i>OrderedDict</i> and <i>Counter</i>
skrebbelover 13 years ago
&#62; <i>In my mind, dynamic typing = bugs, interpreted language = slow. Ahhhhh!</i><p>He explained why he changed his mind on performance, but not whether or not "dynamic typing = bugs". This would've been interesting to read about from a recent convert.
grantjgordonover 13 years ago
Thanks for posting these tips!