defaultdict

Defaultdict with values defaulted to negative infinity

大城市里の小女人 提交于 2019-12-13 08:28:30
问题 I want to create a default dictionary in which the default values are negative infinity. I tried doing defaultdict(float("-inf")) but it is not working. How do I do this? 回答1: As the traceback specifically tells you : >>> from collections import defaultdict >>> dct = defaultdict(float('-inf')) Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> dct = defaultdict(float('-inf')) TypeError: first argument must be callable and per the documentation (emphasis mine): If

Using defaultdict to parse multi delimiter file

假如想象 提交于 2019-12-13 02:59:22
问题 I need to parse a file which has contents that look like this: 20 31022550 G 1396 =:0:0.00:0.00:0.00:0:0:0.00:0.00:0.00:0:0.00:0.00:0.00 A:2:60.00:33.00:37.00:2:0:0.02:0.02:40.00:2:0.98:126.00:0.98 C:0:0.00:0.00:0.00:0:0:0.00:0.00:0.00:0:0.00:0.00:0.00 G:1391:60.00:36.08:36.97:719:672:0.51:0.01:7.59:719:0.49:126.00:0.50 T:1:60.00:33.00:37.00:0:1:0.37:0.02:47.00:0:0.00:126.00:0.18 N:0:0.00:0.00:0.00:0:0:0.00:0.00:0.00:0:0.00:0.00:0.00 +A:2:60.00:0.00:37.00:2:0:0.67:0.01:0.00:2:0.65:126.00:0.65

Python defaultdict that does not insert missing values

有些话、适合烂在心里 提交于 2019-12-12 10:29:36
问题 So the defaultdict documentation mentions that, if an item is missing, the value returned by default_factory "is inserted in the dictionary for the key, and returned." That's great most of the time, but what I actually want in this case is for the value to be returned but not inserted into the defaultdict. I figured I could probably subclass defaultdict and override... I guess __missing__ ? Not sure. What's the best way to go about this? Thanks in advance. 回答1: You can subclass dict and

Loading a defaultdict in Hadoop using pickle and sys.stdin

别来无恙 提交于 2019-12-11 17:34:07
问题 I posted a similar question about an hour ago, but have since deleted it after realising I was asking the wrong question. I have the following pickled defaultdict : ccollections defaultdict p0 (c__builtin__ list p1 tp2 Rp3 V"I love that" p4 (lp5 S'05-Aug-13 10:17' p6 aS'05-Aug-13 10:17' When using Hadoop, the input is always read in using: for line in sys.stdin: I tried reading the pickled defaultdict using this: myDict = pickle.load(sys.stdin) for text, date in myDict.iteritems(): But to no

How to combine multiple dicts, summing the values of common keys (and retaining those with value 0) in Python?

不羁岁月 提交于 2019-12-11 13:59:56
问题 Given three dicts d1, d2 and d3: d1 {'a':1,'b':2,'c':3, 'd':0) d2 {'b':76} d3 {'a': 45, 'c':0} There are some key names that are common to more than one dict (and in reality, they will represent the same real-life object). Others such as 'd' in d1 only exist in d2. I want to group all dicts together, first summing the values of the common keys first, ending up with: {'a':46, 'b':78, 'c':3, 'd': 0} If every dict were the same size and contained the same keys, I could do something like:

Iterate over list of dictionaries and find matching elements from a list and append value of matching key to defaultdict

流过昼夜 提交于 2019-12-11 07:51:24
问题 I have a list of dictionaries. Let's call it: list_of_dict . The dictionaries in the list are in the form: {'a' : 1, 'b' : 5, 'c' : 3, 'd' : 6} and {'a' : 3, 'f' : 2, 'g' : 1, 'h' : 3, 'i' : 5, 'j' : 3} I have another list called list_to_match which only holds some letters: ['a', 'f', 'x'] I need to iterate over list_of_dict and find the keys that match with the element in the list and append the values to a an empty defaultdict of list items. If not found, append 0 to the list. The

min() on collections.defaultdict() returns max count

只谈情不闲聊 提交于 2019-12-10 23:35:18
问题 When using min() on a defaultdict object, it strangely returns the maximum if used on a dict counting indices of a string. For example: >>> import collections >>> defaultdict=collections.defaultdict >>> x=defaultdict(int) >>> string="lol I am a lol noob" >>> for k in string: x[k]+=1 >>> x defaultdict(<type 'int'>, {'a': 2, ' ': 5, 'b': 1, 'I': 1, 'm': 1, 'l': 4, 'o': 4, 'n': 1}) >>> min(x.items()) (' ', 5) 回答1: items() returns the items as (key, value) tuples. This means that when they are

How to print defaultdict variable without its type?

坚强是说给别人听的谎言 提交于 2019-12-10 03:12:35
问题 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? 回答1: just transform it to a regular dict print( dict(confusion_proba_dict) ) but if you

defaultdict equivalent for lists [duplicate]

一曲冷凌霜 提交于 2019-12-08 15:48:48
问题 This question already has answers here : creating a defaultlist in python (2 answers) Closed 3 years ago . Is there\How would you build an equivalent of python's very useful collections.defaultdict? Imagined usage of such a container: >>> a = collections.defaultlist(0) >>> a[2]=7 >>> a[4]='x' >>> a [0,0,7,0,'x'] UPDATE: I've added a follow up question to add even more functionality to this construct 回答1: I think this would be a bit confusing to use; however, here's my first thought on how to

Indenting the contents of each level of a nested dictionary

﹥>﹥吖頭↗ 提交于 2019-12-08 11:22:54
问题 I have a nested dictionary that contains bookmarks that were read from csv. Each level of nesting can have sub-folders and bookmarks. I need to make sure that I properly indent the sub-folders and bookmarks when I print them out. With my current code, all sub-folders are indented at the same level. This makes it appear as if my parent folder only has one level of nesting, but this is not the case. My bookmarks should have My code is: with open('urls.csv') as bookmarks_input: reader = csv