On Wednesday, July 3, 2013 4:58:34 PM UTC-7, Sam Math wrote: > > I'm looking for an equivalent to the 'map' function in C++, > http://www.cplusplus.com/reference/map/map/. > > Essentially, I want an associative array. Specifically, I want the array > to be indexed by floats. > > I'm aware of dictionaries in Python, but my program seems to take a lot of > time because the dictionary is not sorted by its key value... it's > unordered. I need to find the "first" (smallest key) element. Currently, I > do this using myDictionary[min(myDictionary.keys())]. > > For example, > > myDictionary={1.1:1, 1.5:2, 0.9:3} > myDictionary[min(myDictionary.keys())] > > will print out 3. > > But calling the "min" function takes time for large arrays, and I figured > if the array was constructed in an ordered manner, it would be more > efficient (even though insert elements would take more time as well), which > is why I'm looking for the equivalent to the map command in C++. >
How about using an ordered dictionary? (http://docs.python.org/2/library/collections.html#ordereddict-objects) sage: from collections import OrderedDict sage: d={1.1:1, 1.5:2, 0.9:3} sage: od = OrderedDict(sorted(d.items(), key=lambda t: t[0])) sage: od OrderedDict([(0.900000000000000, 3), (1.10000000000000, 1), (1.50000000000000, 2)]) sage: od[1.1] 1 -- John -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at http://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/groups/opt_out.