top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Python: Dictionaries with variable default

+2 votes
328 views

Is it possible to have a default dictionary where the default is dependent on the key?

I was hoping something like this might work:

>>> m = defaultdict(lambda key: key+1)

But it obviously doesn't:

>>> m[3]

Traceback (most recent call last):

 File "", line 1, in 
TypeError: () takes exactly 1 argument (0 given)
posted Nov 3, 2014 by anonymous

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button

Similar Questions
+2 votes

Please share some thoughts of Global Variable In multiprocessing with examples for python.
Do we have some best practices of multiprocessing.

+1 vote

How will you create a dictionary in python?
How will you get all the keys from the dictionary?
How will you get all the values from the dictionary?

+1 vote

My code has this structure:

class Example(wx.Frame,listmix.ColumnSorterMixin):
 def __init__(self,parent):
 wx.Frame.__init__(self,parent)

 self.InitUI()

 def InitUI(self): 
 ..... 

when a button is clicked this function is called and i take the self.id_number which is a number

 def OnB(self, event):
 self.id_number = self.text_ctrl_number.GetValue()
 aa = latitude[int(self.id_number)]
 bb = longitude[int(self.id_number)]

I want to pass the variables aa and bb to a different script called application. This script by calling it with import, automatically pop up a window. I need by clicking the button that is linked with OnB definition to pop up the window from the other script as it does when i am running it alone and display lets say for example the variables aa and bb, how can I do it

+5 votes

Using 1/3 as an example,

 >>> 1./3
0.3333333333333333
 >>> print "%.50f" % (1./3)
0.33333333333333331482961625624739099293947219848633
 >>> print "%.50f" % (10./3)
3.33333333333333348136306995002087205648422241210938
 >>> print "%.50f" % (100./3)
33.33333333333333570180911920033395290374755859375000

which seems to mean real (at least default) decimal precision is limited to "double", 16 digit precision (with rounding error). Is there a way to increase the real precision, preferably as the default?
For instance, UBasic uses a "Words for fractionals", f, "Point(f)" system, where Point(f) sets the decimal display precision, .1^int(ln(65536^73)/ln(10)), with the last few digits usually garbage.
Using "90*(pi/180)*180/pi" as an example to highlight the rounding error (4 = UBasic's f default value):

 Point(2)=.1^09: 89.999999306
 Point(3)=.1^14: 89.9999999999944
 Point(4)=.1^19: 89.9999999999999998772
 Point(5)=.1^24: 89.999999999999999999999217
 Point(7)=.1^33: 89.999999999999999999999999999999823
 Point(10)=.1^48: 89.999999999999999999999999999999999999999999997686
 Point(11)=.1^52: 89.9999999999999999999999999999999999999999999999999632

If not in the core program, is there a higher decimal precision module that can be added?

...