A more pythonic way of writing this dictionary loop?

25 views
Skip to first unread message

flaye

unread,
Oct 16, 2014, 1:10:18 AM10/16/14
to python_in...@googlegroups.com
Is there a cleaner, more efficient way of writing this dictionary loop?

jntsDict={}
jntsDict
["leg"]=[["L_upLeg_JNT","L_knee_JNT","L_ankle_JNT"],
         
["R_upLeg_JNT","R_knee_JNT","R_ankle_JNT"]]
jntsDict
["arm"]=[["L_upArm_JNT","L_elbow_JNT","L_wrist_JNT"],
         
["R_uparm_JNT","R_elbow_JNT","R_wrst_JNT"]]


for i,v in enumerate(jntsDict):
   
for x in range(len(jntsDict[v])):
       
for y in range(len(jntsDict[v][x])):
           
# print every value in the keys, to be used in rigging setup
           
print jntsDict[v][x][y]

Thanks.

damon shelton

unread,
Oct 16, 2014, 3:21:28 AM10/16/14
to python_in...@googlegroups.com
I gave a few different variations that actually might help on speeding up accessibility

#this method is less code and uses the dictionary object functionality and iterates through each list. with range you are creating an actual list of integers for that len in memory
for key, value in jntsDict.items():
for v in value:
for item in v:
print(item)

if you want a faster way to store and access info inside the dictionary, the here are some options

#nested dictionary and use sides as keys
jntsDict={}
jntsDict["leg"]={"left":["L_upLeg_JNT","L_knee_JNT","L_ankle_JNT"],
"right":["R_upLeg_JNT","R_knee_JNT","R_ankle_JNT"]}
jntsDict["arm"]={"left":["L_upArm_JNT","L_elbow_JNT","L_wrist_JNT"],
          "right":["R_uparm_JNT","R_elbow_JNT","R_wrst_JNT"]}
for key, value in jntsDict.items():
for side in ['left', 'right']:
for v in value[side]:
print(v)
#or just store joint names and iterate through sides list and prefix the joint names
jntsDict={}
jntsDict["leg"]=["upLeg_JNT","knee_JNT","ankle_JNT"]
jntsDict["arm"]=["upArm_JNT","elbow_JNT","wrist_JNT"]
for key, value in jntsDict.items():
for side in ['L', 'R']:
for v in value:
print('%s_%s'%(side, v))

#or use more key information stored with each item as information so something like a quadruped could have different sides or center joints that don't have sides can be parsed in the same dictionary
jntsDict={} jntsDict["spine"]={'joints':["spine_a_JNT","spine_b_JNT","spine_c_JNT"]} jntsDict["leg"]={'sides':['L', 'R'], 'joints':["upLeg_JNT","knee_JNT","ankle_JNT"]} jntsDict["arm"]={'sides':['L', 'R'], 'joints':["upArm_JNT","elbow_JNT","wrist_JNT"]} jntsDict["quadleg"]={'sides':['FL', 'FR', 'BL', 'BR'], 'joints':["upLeg_JNT","knee_JNT","ankle_JNT"]} for key,value in jntsDict.items(): if value.get('sides'): for side in value.get('sides'): for j in value.get('joints'): print('%s_%s'%(side, j)) else: for j in value.get('joints'): print(j)

p.s. sorry for mixed single and double quotes, I would normally match them all but was just inserting code into yours and I use single quotes as my standard because I find that double quotes tend to be nested inside of strings more than single quotes

hope this helps
-Damon

--
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/25bc096a-6472-4ee1-adc8-14482431b547%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

flaye

unread,
Oct 16, 2014, 9:24:46 AM10/16/14
to python_in...@googlegroups.com
Damon, that's great! Thank you very much for those nuggets of knowledge. Very much appreciated.
Reply all
Reply to author
Forward
0 new messages