Dictionaries are basically hash maps and therefore are not ordered. You
can't preserve the order of the entries in a dict, indeed when you use sort
you are doing it on a list. There is something called ordered dictionary
that you can check out, otherwise you could use a list of tuples. I hope
it helps.
Il giorno martedì 11 dicembre 2018 21:07:52 UTC+1, yann19 ha scritto:
>
> I apologize in advance as this is not much of a Maya question but I am
> trying to convert this 'list' within nested dictionaries into a dictionary
> type.
>
> I had a function where it ingests in a nested dictionary and allows me to
> sort the contents by a specified value eg. name.
> This is a portion of it -
> return {k: {'entries': sorted([v1 for _,v1 in v['entries'].items()], key=
> lambda el: el['name'])} for k,v in dict_data.items()}
>
> However, possibly because I am using [] which returns me a list type.
>
> What I am expecting in the output is something as follow:
> {'my test':
> {
> 'entries': {
> 'name': 'running_slow_v001'
> ...
> },
> ...
> }
> }
>
> but I am getting:
> {'my test':
> {
> 'entries': [{
> 'name': 'running_slow_v001'
> ...
> }],
> ...
> }
> }
>
>
> I tried using `dict()` and tested the placement anywhere within the return
> sentence, it will returns me an error.
> Appreciate in advance for any replies.
>
>
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/8bb4b3e2-864b-40de-b977-589d0a16e6e1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.