setdefault

python dict: get vs setdefault

限于喜欢 提交于 2019-12-28 05:06:47
问题 The following two expressions seem equivalent to me. Which one is preferable? data = [('a', 1), ('b', 1), ('b', 2)] d1 = {} d2 = {} for key, val in data: # variant 1) d1[key] = d1.get(key, []) + [val] # variant 2) d2.setdefault(key, []).append(val) The results are the same but which version is better or rather more pythonic? Personally I find version 2 harder to understand, as to me setdefault is very tricky to grasp. If I understand correctly, it looks for the value of "key" in the

Re-structuring a list of Python Dicts using setdefault

柔情痞子 提交于 2019-12-24 12:47:58
问题 I am trying to re-structure a list of Python dictionaries by 'grouping' (that's probably not the correct expression, but using it as a proxy based on SQL) the dictionaries based on a (non-unique) value. I have got close, however I'm falling at the final hurdle, in that I couldn't work out how to re-assign each value to a name (i.e. I end up with what looks like a tuple rather than a dict). Additionally I have a superfluous list (i.e. my output ends up as [[{...}]] rather than [{...}]. I have

setdefault vs defaultdict performance

让人想犯罪 __ 提交于 2019-12-21 03:58:22
问题 I am writing code for an application where performance is important. I am wondering why defaultdict seems to be faster then setdefault . I would like to be able to use setdefault , mostly because i do not like the print output of the nested defaultdict (see implementation below). In my code, i need to test if element_id is already a key of the dict. Here are the two functions that i am testing: def defaultdictfunc(subcases,other_ids,element_ids): dict_name= defaultdict(lambda: defaultdict

Is the defaultdict in Python's collections module really faster than using setdefault?

若如初见. 提交于 2019-12-04 08:20:51
问题 I've seen other Python programmers use defaultdict from the collections module for the following use case: from collections import defaultdict s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] def main(): d = defaultdict(list) for k, v in s: d[k].append(v) I've typically approached this problem by using setdefault instead: def main(): d = {} for k, v in s: d.setdefault(k, []).append(v) The docs do in fact claim that using defaultdict is faster, but I've seen the

setdefault vs defaultdict performance

a 夏天 提交于 2019-12-04 02:48:29
I am writing code for an application where performance is important. I am wondering why defaultdict seems to be faster then setdefault . I would like to be able to use setdefault , mostly because i do not like the print output of the nested defaultdict (see implementation below). In my code, i need to test if element_id is already a key of the dict. Here are the two functions that i am testing: def defaultdictfunc(subcases,other_ids,element_ids): dict_name= defaultdict(lambda: defaultdict(lambda: defaultdict(dict))) for subcase in subcases: for other_id in other_ids: for element_id in element

Is the defaultdict in Python's collections module really faster than using setdefault?

ぃ、小莉子 提交于 2019-12-03 00:22:34
I've seen other Python programmers use defaultdict from the collections module for the following use case: from collections import defaultdict s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] def main(): d = defaultdict(list) for k, v in s: d[k].append(v) I've typically approached this problem by using setdefault instead: def main(): d = {} for k, v in s: d.setdefault(k, []).append(v) The docs do in fact claim that using defaultdict is faster , but I've seen the opposite to be true when testing myself: $ python -mtimeit -s "from withsetdefault import main; s = [('yellow

python dict: get vs setdefault

落花浮王杯 提交于 2019-11-28 05:14:35
The following two expressions seem equivalent to me. Which one is preferable? data = [('a', 1), ('b', 1), ('b', 2)] d1 = {} d2 = {} for key, val in data: # variant 1) d1[key] = d1.get(key, []) + [val] # variant 2) d2.setdefault(key, []).append(val) The results are the same but which version is better or rather more pythonic? Personally I find version 2 harder to understand, as to me setdefault is very tricky to grasp. If I understand correctly, it looks for the value of "key" in the dictionary, if not available, enters "[]" into the dict, returns a reference to either the value or "[]" and

Why does this python dictionary get created out of order using setdefault()?

試著忘記壹切 提交于 2019-11-26 17:28:51
问题 I'm just starting to play around with Python (VBA background). Why does this dictionary get created out of order? Shouldn't it be a:1, b:2...etc.? class Card: def county(self): c = 0 l = 0 groupL = {} # groupL for Loop for n in range(0,13): c += 1 l = chr(n+97) groupL.setdefault(l,c) return groupL pick_card = Card() group = pick_card.county() print group here's the output: {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'g': 7, 'f': 6, 'i': 9, 'h': 8, 'k': 11, 'j': 10, 'm': 13, 'l': 12} or, does it

Use cases for the 'setdefault' dict method

倖福魔咒の 提交于 2019-11-26 01:48:26
问题 The addition of collections.defaultdict in Python 2.5 greatly reduced the need for dict \'s setdefault method. This question is for our collective education: What is setdefault still useful for, today in Python 2.6/2.7? What popular use cases of setdefault were superseded with collections.defaultdict ? 回答1: You could say defaultdict is useful for settings defaults before filling the dict and setdefault is useful for setting defaults while or after filling the dict . Probably the most common