So I'm not sure Ordered Dictionaries will solve my pforoblem.
To clarify what I'm doing...
I'm running a loop where, with each iteration, I'm adding 2 elements to the
dictionary (each with a specific key that's a float). In addition, I'm
removing the element with the smallest key (again, it's a float).
Currently, I implement it like this:
d = {0:0}
for i in range(1,10000):
d[random()]=1
d[random()]=1
del d[min(d.keys())]
So as you can see, I'm adding 2 elements (in this case, the values are 1...
but I have specific values in my program) to the dictionary, but removing
the "first" (in the sense of its key). Calling the "min" function seems to
be taking time, which is understandable.
>From what I understand, if I use an Ordered Dictionaries won't help me
because I'll have to sort it every time, which I assume takes time.
So I'm looking for a more efficient solution. I feel if when appending to
the dictionary, if it's inserted in a sorted manner, the program will run
much faster, since I can just call the "first" element. So the only thing
that would take time is the data insertion into the dictionary. Now, it may
be the case that inserting the data takes the same amount of time as
finding the minimum key, but I'm not sure.
Thanks
On Wednesday, July 3, 2013 8:07:14 PM UTC-5, John H Palmieri wrote:
>
>
>
> 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/groups/opt_out.