[Tutor] A sorted dictionary (of sorts)
Mats Wichmann
mats at wichmann.us
Thu Sep 29 13:39:39 EDT 2022
On 9/29/22 08:29, Ed Connell wrote:
> Hi,
>
> The following works fine, but it always made me nervous because
> ordinary dictionaries are not supposed to be sorted. It works (for
> now). Should I worry that some new version of python will break things?
>
> # produce a sorted dictionary
> sorted_names = sorted( Team_Calc_d, key = lambda e: Team_Calc_d[ e
> ].current_power,
> reverse = True ) # sorted by current power
> TeamPwrSorted_Result_d = {}
> for tm in sorted_names:
> # setup SortTeams_PowerResult_Dict and copy from Analysis objects
> # to Results objects
> TeamPwrSorted_Result_d[ tm ] = Results( Team_Basics_d[ tm
> ].start_power )
> # create the dictionary
> TeamPwrSorted_Result_d[ tm ].current_power = Team_Calc_d[ tm
> ].current_power
>
> needs_processing = False # we just updated
>
> return TeamPwrSorted_Result_d
>
It's unlikely anything will breaks because you aren't using internals.
If you want to "check your work" against a fairly well known
implementation (which may have different aims than yours), take a look
at Grant Jenks' stuff.
https://grantjenks.com/docs/sortedcontainers/sorteddict.html
More information about the Tutor
mailing list