defaultdict

Accessing key in factory of defaultdict

夙愿已清 提交于 2019-12-21 07:34:28
问题 I am trying to do something similar to this: from collections import defaultdict import hashlib def factory(): key = 'aaa' return { 'key-md5' : hashlib.md5('%s' % (key)).hexdigest() } a = defaultdict(factory) print a['aaa'] (actually, the reason why I need access to the key in the factory is not to compute an md5 , but for other reasons; this is just an example) As you can see, in the factory I have no access to the key: I am just forcing it, which makes no sense whatsoever. Is it possible to

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

TypeError: first argument must be callable, defaultdict

廉价感情. 提交于 2019-12-20 04:03:12
问题 The error comes from publishDB = defaultdict(defaultdict({})) I want to make a database like {subject1:{student_id:{assignemt1:marks, assignment2:marks,finals:marks}} , {student_id:{assignemt1:marks, assignment2:marks,finals:marks}}, subject2:{student_id:{assignemt1:marks, assignment2:marks,finals:marks}} , {student_id:{assignemt1:marks, assignment2:marks,finals:marks}}} . I was trying to populate it as DB[math][10001] = a dict and later read out as d = DB[math][10001] . Since, I am on my

Exposing `defaultdict` as a regular `dict`

℡╲_俬逩灬. 提交于 2019-12-18 11:43:38
问题 I am using defaultdict(set) to populate an internal mapping in a very large data structure. After it's populated, the whole structure (including the mapping) is exposed to the client code. At that point, I don't want anyone modifying the mapping. And nobody does, intentionally. But sometimes, client code may by accident refer to an element that doesn't exist. At that point, a normal dictionary would have raised KeyError , but since the mapping is defaultdict , it simply creates a new element

python defaultdict: 0 vs. int and [] vs list

我只是一个虾纸丫 提交于 2019-12-18 11:41:23
问题 Is there any difference between passing int and lambda: 0 as arguments? Or between list and lambda: [] ? It looks like they do the same thing: from collections import defaultdict dint1 = defaultdict(lambda: 0) dint2 = defaultdict(int) dlist1 = defaultdict(lambda: []) dlist2 = defaultdict(list) for ch in 'abracadabra': dint1[ch] += 1 dint2[ch] += 1 dlist1[ch].append(1) dlist2[ch].append(1) print dint1.items() print dint2.items() print dlist1.items() print dlist2.items() ## -- Output: -- [('a',

Python: convert defaultdict to dict

最后都变了- 提交于 2019-12-17 08:55:30
问题 How can i convert a defaultdict number_to_letter defaultdict(<class 'list'>, {'2': ['a'], '3': ['b'], '1': ['b', 'a']}) to be a common dict? {'2': ['a'], '3': ['b'], '1': ['b', 'a']} 回答1: You can simply call dict : >>> a defaultdict(<type 'list'>, {'1': ['b', 'a'], '3': ['b'], '2': ['a']}) >>> dict(a) {'1': ['b', 'a'], '3': ['b'], '2': ['a']} but remember that a defaultdict is a dict: >>> isinstance(a, dict) True just with slightly different behaviour, in that when you try access a key which

Python: convert defaultdict to dict

女生的网名这么多〃 提交于 2019-12-17 08:55:06
问题 How can i convert a defaultdict number_to_letter defaultdict(<class 'list'>, {'2': ['a'], '3': ['b'], '1': ['b', 'a']}) to be a common dict? {'2': ['a'], '3': ['b'], '1': ['b', 'a']} 回答1: You can simply call dict : >>> a defaultdict(<type 'list'>, {'1': ['b', 'a'], '3': ['b'], '2': ['a']}) >>> dict(a) {'1': ['b', 'a'], '3': ['b'], '2': ['a']} but remember that a defaultdict is a dict: >>> isinstance(a, dict) True just with slightly different behaviour, in that when you try access a key which

How to extend OrderedDict with defaultdict behavior

こ雲淡風輕ζ 提交于 2019-12-13 15:05:07
问题 I have a list of orderdicts. And I would like to combine all of them together and then sort them by the fruit attribute in each of them. I have been to combine and sort them using defaultdict through the code below. super_dict_apple = defaultdict(list) super_dict_orange = defaultdict(list) super_dict_no_fruit = defaultdict(list) for d in dict: if 'fruit' not in d: for k, v in d.iteritems(): super_dict_no_fruit[k].append(v) elif d['fruit'] == 'Apple': for k, v in d.iteritems(): super_dict

python collections.defaultdict() compile error

荒凉一梦 提交于 2019-12-13 14:04:31
问题 The following code, simple and clear enough, produces an error when compiled: import string import collections #create dictionary with alphabets as keys, and empty values list = ['aema', 'airplane', 'amend'] gen_dict = dict.fromkeys(string.ascii_lowercase, '') gen_dict = collections.defaultdict(list) for x in list: gen_dict['a'].append(x) and the error produced is: Traceback (most recent call last): File "indexdict.py", line 14, in <module> gen_dict = collections.defaultdict(list) TypeError:

PEP 8 warning “Do not use a lambda expression use a def” for a defaultdict lambda expression

牧云@^-^@ 提交于 2019-12-13 11:30:01
问题 I am using the below python code to create a dictionary.But I am getting one PEP 8 warning for the dct_structure variable. Warning is: do not use a lambda expression use a def from collections import defaultdict dct_structure = lambda: defaultdict(dct_structure) dct = dct_structure() dct['protocol']['tcp'] = 'reliable' dct['protocol']['udp'] = 'unreliable' I am not comfortable with python lambda expression yet. So can anyone help me to define the function for the below two line of python code