You probably just want to google about recursive looping over data
structures. i.e. here is one that deals with nested dictionaries:
http://stackoverflow.com/questions/10756427/loop-through-all-nested-dictionary-values

Unfortunately, the more unpredictable your data structures are, the more
work you have to put into handling the walking of the structure. Are you
sure these unpredictable nested data structures are the most efficient way
to store your data? I imagine if its difficult for you to efficiently walk
it to clean it up, that it would be equally difficult for the users of your
data structure? (Also, your data structure in your example isn't valid
python).



On Tue, Nov 5, 2013 at 11:13 PM, Simen Chris <[email protected]> wrote:

> Sorry for the delayed response guys
>
> damonshelton: I couldn't get that to work, I'm getting # Error: NameError:
> file <maya console> line 1: name 'key' is not defined #
> Justin Israel: Ideally I would like to remove the entire tuple from the
> list if it doesn't have any members, but just removing each single item
> that doesn't exists would work for me also. Yeah that's the tricky part,
> this isn't always the complete data structure of the dictionaries, if
> possible I would like it to work on dictionaries with other variations.
>
> On Wednesday, October 30, 2013 8:15:13 PM UTC+1, Justin Israel wrote:
>
>> You have single element tuples in your lists. Do you want to remove the
>> tuple from the list if its single member does not exist, or remove the
>> single item leaving an empty tuple? Will there ever be more than one
>> element in the tuple? Is this the complete example of the data structure
>> and the code doesn't have to handle any other variations?
>>  On Oct 31, 2013 4:43 AM, "damon shelton" <[email protected]> wrote:
>>
>>> Use the get function and provide a default value where necessary.
>>> Clearing out stuff would just be adding time to your processing.
>>>
>>> foo = mydict.get(key, None)
>>>
>>> Do that once then use the variable instead of getting the key again. No
>>> need to over complicate your code
>>> On Oct 30, 2013 4:41 AM, "Simen Chris" <[email protected]> wrote:
>>>
>>>>
>>>>
>>>> Hey guys, I often use dictionary in my functions to store and return
>>>> created elements, however, it is really a pain to deal with if something in
>>>> the dictionary doesn't exist. So I would like to have a function that goes
>>>> through all of the elements in a dictionary, and if it finds object's that
>>>> doesn't exists it should remove the values from the dictionary, and if it
>>>> finds empty keys, it should delete them also.
>>>>
>>>> Here's what I got so far:
>>>> #This is my dictionary
>>>> myDict['objects']:  [('pSphere1'),
>>>>                      ('pSphere2'),
>>>>                     [('pCube1'), ('pCube2'), ('pCube3')],
>>>>                     [[('loc1'), ('loc2')], [('obj1'), ('obj2')]],
>>>> myDict['nodes']:    [('pointConstraint1'),
>>>>                      ('pointConstraint2'),
>>>>                      ('pointConstraint3'),
>>>>
>>>> #I want to go through each item in the dictionary
>>>> for key, val in myDict.iteritems():
>>>>     #If the current value is a list
>>>>     if isinstance(val, list) == True:
>>>>         #For each item in the list
>>>>         for valLoop1 in val:
>>>>             #If the current value is a list
>>>>             if isinstance(valLoop1, list) == True:
>>>>                 #For each item in the list
>>>>                 for valLoop2 in valLoop1:
>>>>                   #If the current value is a list
>>>>                   if isinstance(valLoop2, list) == True:
>>>>                     #And this goes on and on
>>>>                   #If the current item is not a list
>>>>                   else:
>>>>                     #If the object doesn't exist
>>>>                     if not cmds.objExists(val):
>>>>                       #Pop the value out from the dictionary
>>>>                       #Don't know how to do this yet
>>>>             #If the current item is not a list
>>>>             else:
>>>>               #If the object doesn't exist
>>>>               if not cmds.objExists(val):
>>>>                 #Pop the value out from the dictionary
>>>>                 #Don't know how to do this yet
>>>>     #If the current item is not a list
>>>>     else:
>>>>       #If the object doesn't exist
>>>>       if not cmds.objExists(val):
>>>>         #Pop the value out from the dictionary
>>>>         #Don't know how to do this yet
>>>>
>>>> http://pastebin.com/2Q6dzLut
>>>>
>>>> This is pretty ugly, how many lists it supports is dependant on how
>>>> many isinstances I write.. Also if I find an object that doesn't exist, I
>>>> don't know how to pop that specific value from the dictionary, and also if
>>>> it finds empty keys..
>>>>
>>>>
>>>> Does anyone have any tips or hints on this?
>>>>
>>>> --
>>>> 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/df9f7cba-cc97-417e-b084-
>>>> da18c99a2485%40googlegroups.com.
>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>
>>>  --
>>> 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/CAM9RXo%2BQa2ZOD3OqQCPd9f%3Dw-
>>> kPnwbrOHXnfeEt2C%3DxdKijeyQ%40mail.gmail.com.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>  --
> 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/36f91e3e-89cb-405f-aef2-0ce0b228c0fc%40googlegroups.com
> .
>
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
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/CAPGFgA1MUfst-eAtv62DYW22c6LVLUe7duxO0bZPKoJkMissZQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to