defaultdict

How to print defaultdict variable without its type?

心不动则不痛 提交于 2019-12-05 03:26:04
In the following code: from collections import defaultdict confusion_proba_dict = defaultdict(float) for i in xrange(10): confusion_proba_dict[i] = i + 10 print confusion_proba_dict Output is: defaultdict(<type 'float'>, {0: 10, 1: 11, 2: 12, 3: 13, 4: 14, 5: 15, 6: 16, 7: 17, 8: 18, 9: 19}) But, I need output to be: {0: 10, 1: 11, 2: 12, 3: 13, 4: 14, 5: 15, 6: 16, 7: 17, 8: 18, 9: 19} How can I do it? just transform it to a regular dict print( dict(confusion_proba_dict) ) but if you are going to use like that, just use a regular dict as you don't use any of the advantages of defaultdict

One-step initialization of defaultdict that appends to list?

不问归期 提交于 2019-12-04 23:31:38
It would be convenient if a defaultdict could be initialized along the following lines d = defaultdict(list, (('a', 1), ('b', 2), ('c', 3), ('d', 4), ('a', 2), ('b', 3))) to produce defaultdict(<type 'list'>, {'a': [1, 2], 'c': [3], 'b': [2, 3], 'd': [4]}) Instead, I get defaultdict(<type 'list'>, {'a': 2, 'c': 3, 'b': 3, 'd': 4}) To get what I need, I end up having to do this: d = defaultdict(list) for x, y in (('a', 1), ('b', 2), ('c', 3), ('d', 4), ('a', 2), ('b', 3)): d[x].append(y) This is IMO one step more than should be necessary, am I missing something here? the behavior you describe

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

How are Counter / defaultdict ordered in Python 3.7?

柔情痞子 提交于 2019-12-04 03:10:05
问题 We know in Python 3.6 dictionaries are insertion ordered as an implementation detail, and in 3.7 insertion ordering can be relied upon. I expected this to also be the case for subclasses of dict such as collections.Counter and collections.defaultdict . But this appears to only hold true for the defaultdict case. So my questions are: Is it true that ordering is maintained for defaultdict but not for Counter ? And, if so, is there a straightforward explanation? Should ordering of these dict

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

defaultdict : first argument must be callable or None

戏子无情 提交于 2019-12-04 02:26:25
I ran the following code : from collections import defaultdict lst = list(range(0,5)) d = defaultdict(lst) and I got this error : TypeError: first argument must be callable or None Please help For a defaultdict the default value is usually not really a value , it a factory : a method that generates a new value. You can solve this issue by using a lambda expression that generates a list: lst = lambda: list(range(0,5)) d = defaultdict(lst) This is also a good idea here, since otherwise all default values would reference the same list . For instance here: d[1].append(14) will not have impact on d

Accessing key in factory of defaultdict

我的梦境 提交于 2019-12-04 02:13:56
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 use defaultdict in a way that I can access the key in the factory? __missing__ of defaultdict does not

Why can't I create a default, ordered dict by inheriting OrderedDict and defaultdict?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 01:28:09
My first attempt to combine the features of two dictionaries in the collections module was to create a class that inherits them: from collections import OrderedDict, defaultdict class DefaultOrderedDict(defaultdict, OrderedDict): def __init__(self, default_factory=None, *a, **kw): super().__init__(default_factory, *a, **kw) However, I cannot assign an item to this dictionary: d = DefaultOrderedDict(lambda: 0) d['a'] = 1 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib64/python3.3/collections/__init__.py", line 64, in __setitem__ self.__map[key] = link =

How to read two lines from a file and create dynamics keys in a for-loop, a follow-up

泄露秘密 提交于 2019-12-03 12:37:50
This question follows the problem in question: How to read two lines from a file and create dynamics keys in a for-loop? But, the nature of the problem has evolved to certain complexity that I want to address. Below is the structure of my data separated by space. chr pos M1 M2 Mk Mg1 F1_hybrid F1_PG F1_block S1 Sk1 S2 Sj 2 16229767 T/T T/T T/T G/T C|T 1|0 726 . T/C T/C T/C 2 16229783 C/C C/C C/C A/C G|C 0|1 726 G/C G/C G/C C|G 2 16229992 A/A A/A A/A G/A G|A 1|0 726 A/A A/A A/A A|G 2 16230007 T/T T/T T/T A/T A|T 1|0 726 A|T A|T A|T A|T 2 16230011 G/G G/G G/G G/G C|G 1|0 726 G/C C|G C|G G/C 2

Using defaultdict with multiprocessing?

我与影子孤独终老i 提交于 2019-12-03 09:38:57
问题 Just experimenting and learning, and I know how to create a shared dictionary that can be accessed with multiple proceses but I'm not sure how to keep the dict synced. defaultdict , I believe, illustrates the problem I'm having. from collections import defaultdict from multiprocessing import Pool, Manager, Process #test without multiprocessing s = 'mississippi' d = defaultdict(int) for k in s: d[k] += 1 print d.items() # Success! result: [('i', 4), ('p', 2), ('s', 4), ('m', 1)] print '*'*10,