Re: [Maya-Python] Converting list entries in nested dictionaries into dictionary type

2018-12-11 Thread Justin Israel
On Wed, Dec 12, 2018 at 9:07 AM yann19  wrote:

> 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()}
>
>
This is a very difficult line to visually parse. At least if the key
function were extracted to its own variable, and if you used
v['entries'].values() then it would be a bit shorter and easier to read.


> 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.
>

It sounds like your single line is too complicated if you are just trying
to throw a dict() call into random places in the expression to see if it
works. It might benefit you to unroll this into a proper for loop. But yea,
the reason you are seeing a list is because sorted() takes an interable and
returns a list. From the looks of your example output it appears that each
value of v['entries'] is a dictionary. That means you will have to further
iterate those values to combine them into a single dictionary. One more
complex way to do it in a single line would be to replace the sorted() with:

{k:v for k,v in intertools.chain.from_iterable(d.items() for d in
v['entries'].values())}

But this only further complicates your already complex single line. This is
equivalent to:

entries = {}
for d in v['entries'].values():
entries.update(d)

But as I look at this formatting goal you mentioned, how do you plan to
handle the collisions of the fields in each 'entries' dict value? Is each
one going to have a 'name' field? If so, they are all going to overlay and
the last key wins. The list format would preserve the individual fields of
each entry.


>
> --
> 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 python_inside_maya+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/8f6ab8cf-b239-4713-8ef7-3deeb86075dd%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0e0FJiUorCFFVZgcO-fPkC4UUhrTovXGERAOq41u%2Br3g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[Maya-Python] Converting list entries in nested dictionaries into dictionary type

2018-12-11 Thread yann19
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 python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/8f6ab8cf-b239-4713-8ef7-3deeb86075dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.