4 ms·
Wow - that's really, really great list. In particular, #7 is something that I didn't even know existed, and I've been hacking around for 2+ years. Instead of:
by ghshephard 12y ago
Wow - that's really, really great list.
In particular, #7 is something that I didn't even know existed, and I've been hacking around for 2+ years.
Instead of:
mdict={'gordon':10,'tim':20}
>>> print mdict.get('gordon',0)
10
>>> print mdict.get('tim',0)
20
>>> print mdict.get('george',0)
0
I've always done the much more verbose:
class defaultdict(dict):
def __init__(self, default=None):
dict.__init__(self)
self.default = default
def __getitem__(self, key):
try:
return dict.__getitem__(self, key)
except KeyError:
return self.default
mdict=defaultdict(0)
mdict['gordon']=10
mdict['tim']=20
print mdict['gordon']
10
print mdict['tim']
20
print mdict['george']
0
I'll be sure to make great use of the dictionary get method - I'm embarrassed to admit how many thousands of times I could have used that, and didn't know it existed.
- bhy 12y agoDo you know there's also collections.defaultdict ?
- ghshephard 12y agoI do now! I had known (and used) collections.OrderedDict, but had never used defaultdict. Millions of keyerrors later... I clearly am going to have to spend a few hours today grokking everything that collections.* has to offer. Thanks very much.
- raverbashing 12y agoRead the manuals! ;) There's a lot of hidden gems. Your idea is nice in a syntactic sugar way, also, the default being a part of the dictionary rather than the get function makes it copyable. I'll give you another gem that could be interesting: else clause in for loops
- surreal 12y agoAlso read other people's code :) You'll see this being used countless times in libraries / on GitHub.
- myg204 12y agoAnother good/great source of Python tricks/idioms is Raymond Hettinger's "Idiomatic Python". The slides/videos are really great. I highly recommend them. https://speakerdeck.com/pyconslides/transforming-code-into-beautiful-idiomatic-python-by-raymond-hettinger-1 https://speakerdeck.com/pyconslides/transforming-code-into-b...
- europa 12y agoYour defaultdict approach and the dict.get with a default specified is not really equivalent. In the defaultdict case when you encounter a non existing key it adds a new entry with that key into the dict. i.e. your dict will start growing. whereas dic.get with default value keep returning you the default value without touching your dict.
- ghshephard 12y agore: "Your defaultdict approach and the dict.get with a default specified is not really equivalent. In the defaultdict case when you encounter a non existing key it adds a new entry with that key into the dict. i.e. your dict will start growing." europa - The dictionary is only modified if you are using a method to modify it. When you are just passively querying it, it's not impacted. class defaultdict(dict): def __init__(self, default=None): dict.__init__(self) self.default = default def __getitem__(self, key): try: return dict.__getitem__(self, key) except KeyError: return self.default mdict=defaultdict(0) mdict['gordon']=10 mdict['tim']=20 print mdict['gordon'] print mdict['tim'] print mdict['george'] print mdict 10 20 0 {'tim': 20, 'gordon': 10}
- europa 12y agoYou are right about your implementation. I was thinking about the Pythons collections.defaultdict from collections import defaultdict s = 'mississippi' d = defaultdict(int) for k in s: d[k] += 1 d.items() [('i', 4), ('p', 2), ('s', 4), ('m', 1)]
- ghshephard 12y agoThe difference here - is that you are actually assigning a value to the dictionary element. It's the assignment that's growing the dictionary, not the query.