return {k: {'entries': sorted([v1 for _,v1 in v['entries'].items()], key=lambda el: el['name'])} for k,v in dict_data.items()}
{'my test':
{
'entries': {
'name': 'running_slow_v001'
...
},
...
}
}
{'my test':
{
'entries': [{
'name': 'running_slow_v001'
...
}],
...
}
}
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_m...@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.
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/8bb4b3e2-864b-40de-b977-589d0a16e6e1%40googlegroups.com.
from datetime import datetime
def sortedPage(d): return {k: {'elements': sorted([v1 for k1 ,v1 in v['elements'].items()], key=lambda el: datetime.strptime(el['data']['created'], '%d/%m/%y %H:%M'))} for k,v in d.items()}
output = {k: sortedPage(v) if k == 'pages' else v for k,v in test_dict.items()}pprint(output)
{'page_order': ['rotatingTest', 'zoomingTest', 'panningTest'], 'pages': {'panningTest': {'elements': [{'data': {'created': '04/10/18 12:43', 'description': 'panning test for posZ', 'name': 'posePan_positionZ_v001', 'project': 'TEST', 'version': '1'}, 'name': 'posePan_positionZ_v001', 'thumbnail': '/user_data/posePan_positionZ_v001/posePan_positionZ_v001.thumb.jpg', 'type': 'PosedWidget', 'uid': '7lyuri8g8u5ctwsa'}]},
{'page_order': ['rotatingTest', 'zoomingTest', 'panningTest'], 'pages': {'panningTest': {'elements': {'7lyuri8g8u5ctwsa' : [{'data': {'created': '04/10/18 12:43', 'description': 'panning test for posZ', 'name': 'posePan_positionZ_v001', 'project': 'TEST', 'version': '1'}, 'name': 'posePan_positionZ_v001', 'thumbnail': '/user_data/posePan_positionZ_v001/posePan_positionZ_v001.thumb.jpg', 'type': 'PosedWidget', 'uid': '7lyuri8g8u5ctwsa'}]}}
--
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_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/6b3f5f28-8898-4570-9448-36b07a405005%40googlegroups.com.