FAQ Database Discussion Community

Assigning values to objects attributes that don't exist

I'm doing a data mining homework with python(2.7). I created a weight dict for all words(that exist in the category), and for the words that don't exist in this dict, i want to assign a default value. First I tried with setdefault for everykey before using it, it works perfectly,...

union of two default dictionaries

am trying to create a union of two default dictionaries. Here is the code for the method in my class: def __add__(self,right): mergedbag = copy.copy(self.bag_value) for item in right: if item not in mergedbag:mergedbag[item] = 0 mergedbag[item] += right[item] return mergedbag I create two default dictionaries: b = Bag(['d','a','b','d','c','b','d']) c...

defaultdict with default value 1?

I am new to python, and i read some code snippet from some place. It's an implementation of counting sort. The code is as below: from collections import defaultdict def sort_colors(A): ht = {} # a hash map ht = defaultdict(lambda:0, ht) # with default value 1 for i in...

defaultdict changes get reflected in list

i was just trying defaultdict and i am not able to understand as to why changing defaultdict (d[1]=2) causes change in the list v ,although appending has been done before the change in value. please help.. >>> d=defaultdict(int) >>> d[1]=1 >>> d[2]=3 >>> v=[] >>> v.append(d) >>> v.append(d) >>> v...

Integers in defaultdict

I want to go through a text file and create a dictionary that has keywords and the number of times they pop up.I want it to sort of look like this: defaultdict(<type 'int'>, {'keyword1': 1, 'keyword2': 0, 'keyword3': 3, 'keyword4': 9}) right now I get something that looks like this:...

Defaultdict with values defaulted to negative infinity

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?

Totaling occurrences of an element in a dictionary of lists

I have this dictionary of lists: d = {'Apple': [['Category1', 14], ['Category2', 12], ['Category2', 8]], 'Orange' : [['Category2', 12], ['Category2', 12], '[Category3', 2]]} I would like the output to be like: d = {'Apple': [['Category1', 14], ['Category2', 20]],'Orange': [['Category2', 24], ['Category3', 2]]} Category i.e Category1, Category2 with the same name...

How to use a DefaultDict with a lambda expression to make the default changeable?

DefaultDicts are useful objects to be able to have a dictionary that can create new keys on the fly with a callable function used to define the default value. eg. Using str to make an empty string the default. >>> food = defaultdict(str) >>> food['apple'] '' You can also use...

issue with equating dictionaries and global issue

Trying to figure best way to union of two dictionaries. Here is the code that I have. Counter is one of the options that I found. def __add__(self,right): mergedbag = Bag() mergedbag.bag_value = copy.copy(self.bag_value) for item in right.bag_value.keys(): mergedbag.bag_value[item] += right.bag_value[item] return mergedbag ...

How to create a specific default list in defaultdict?

I am trying to build a defaultdict which is initialized with a specific list. Such that if I access the dict using a key that does exist, it will be initialized with a specific list, let's say [True, True, True]. Instead of doing this my_defaultdict = collections.defaultdict(list) So, for example,...

Why do you need lambda to nest defaultdict?

I am a bit confused on why you need a lambda function for nesting defaultdict Why can't you do it like this? test = defaultdict(defaultdict(list)) instead of test = defaultdict(lambda:defaultdict(float)) ...

Python DefaultDict Ordering Multiple Values Issue

I am trying to order all of the scores in order of each user's score from highest to lowest. I have attempted this with the code below: import collections from collections import defaultdict from operator import itemgetter worker_scores = collections.defaultdict(lambda: collections.deque(maxlen=3)) import operator worker_scores.clear() with open('score_names.txt') as f: for line...