From: Danny Y. <dy...@hk...> - 2004-06-30 20:27:27
|
On Wed, 30 Jun 2004, In Geol Ok wrote: > In python there is a method called dict(). This method returns a > dictionary from a list. > > For example: > > def returnDict(self): > self.list = [[a, 1], [b, 2], [c, 3]] > d = dict(self.list) > return d > > function returns -> {a:1, b:2, c:3} > > When I try this in jython it gives me a "NameError: dict". I'm guessin > dict() doesn't exist in jython. Any suggestions would be appreciated. Hi In Geol Ok, Yes, the 'dict()' builtin was introduced in CPython 2.2. Although it isn't in Jython 2.1, you can write your own equivalent dict() function: ### def dict(sequence): resultDict = {} for key, value in sequence: resultDict[key] = value return resultDict ### This doesn't do everything that the real dict() function does, but it shouldn't be too hard to add on more features onto the function to make it match the 2.2 builtin more closely. Hope this helps! |