views:

94

answers:

3

What I'm trying to do is this. I have a dictionary laid out as such:

legJointConnectors = {'L_hip_jnt': ['L_knee_jnt'], 'L_knee_jnt': ['L_ankle_jnt'], 'L_ankle_jnt': ['L_ball_jnt'], 'L_ball_jnt': ['L_toe_jnt']}

What I want to be able to do is iterate through this, but change the L_ to R_. Here's how I tried to do it, but it failed, because it's expecting a string and I'm passing it a list.

for key, value in legJointConnectors.iteritems():
    if side == 'L':
        cmds.connectJoint(value, key, pm=True)
    else:
        key = re.sub('L_', 'R_', key)
        value = re.sub('L_', 'R_', value)
        cmds.connectJoint(value, key, pm=True)

Obviously I'm not doing this correctly, so how can I do this? Should I create an empty dictionary and populate it with the necessary data on the fly? Or is there a better way to do it? Thanks for the help!

[edit] The if side == 'L' is testing to see what side we're currently working on. This script is being used within Maya, so I'm creating joints based on the side and then connecting them.

Based off of KennyTM's suggestion I tried this:

for key, value in legJointConnectors.iteritems():
        if side == 'L':
            cmds.connectJoint(value, key, pm=True)
        else:
            for v in value:
                value = 'R_' + v[2:]
                for k in key:    
                    key = 'R_' + k[2:]
                print key
            print value

but while it returns the correct key, the value returns as R_

+2  A: 

Do the substitution on every element of the list then.

for key, value in legJointConnectors.iteritems():
    if side != 'L':
        key = 'R_' + key[2:]
        value = ['R_' + v[2:] for v in value]
    cmds.connectJoint(value, key, pm=True)

(BTW, it is better to use v.replace('L_', 'R_'), or just 'R_' + v[2:] to perform the replacement then using regex for this.)

KennyTM
Well, I just tried all 3 options, but none of them worked for me. I'm positive it's not your answer, but my implementation. I'm fairly new to Python, so I'm sure it's my growing pains. :/
ArrantSquid
@Arrant: :\ . See update.
KennyTM
You are awesome. And after seeing that, I actually understand now, why the value wasn't getting passed (because I wasn't calling it correctly - in other words, it was my implementation as I had already said :/). Again, thanks so much! Python is a beast, but I'm trying to ride it anyways. :) - as an aside - As a beginner to python, I love dictionaries, but they are the thing that I've needed the most help with. It'd be nice if there were some good resources (other than the docs) that explained how to use them effectively (like your example).
ArrantSquid
A: 

You could create a new dictionary:

remap = lambda x: re.sub('L_','R_', x )
dict( remap( key ), [remap( value[0] )] for key, value in mydict.iteritems() )
wheaties
A: 

I would have suggested { key.replace('L_', 'R_'): [value.replace('L_', 'R_') for value in values ] for (key, values) in legJointConnectors.iteritems() } (if you use a python version that lacks dict comprehensions, you can use dict( (key..., [value...]) for ...) instead), but the if side == 'L' part confuses me... what does it do?

delnan
It checks to see which side we're currently working on. I'll add a better description above. :)
ArrantSquid