0

I'm trying to go through a list and print out the list using a secondary list of variables.

The problem is that the variables in the list are getting assigned and set, but are not getting printed out properly. This feels like something really simple but I've been stumped for a day or so on it. Any help would be appreciated!

side = ""
sideLong = ""
sideShort = ""
sideList = ["Right", "Left"]


jointRemapList =[
    ["Char_Hips", "Root_M"],
    ["Char_" + sideLong + "Finger1", "IndexFinger1_" + sideShort],
    ["Char_" + sideLong + "Finger2", "IndexFinger2_" + sideShort]
    ]

for side in sideList:
    sideLong = side
    sideShort = side[0]

    for jointPair in jointRemapList:

        print sideLong, sideShort, jointPair

Expected output would be:

Left L ['Char_Hips', 'Root_M']
Left L ['Char_LeftFinger1', 'IndexFinger1_L']
Left L ['Char_LeftFinger2', 'IndexFinger2_L']
Right R ['Char_Hips', 'Root_M']
Right R ['Char_RightFinger1', 'IndexFinger1_R']
Right R ['Char_RightFinger2', 'IndexFinger2_R']
1
  • Python doesn't keep pointers to variables this way. "foo" + bar is not the string "foo" with the suffix of "whatever bar is when we print it", bar is evaluated immediately and the string is "foo" + "whatever bar is right now." Commented Jun 14, 2017 at 17:22

2 Answers 2

3

I think the problem is happening because you are creating jointRemapList outside of your sideList loop. You want the list to be created dynamically by sideLong and sideShort, so you need to recreate it on each increment of your loop. Like the following:

side = ""
sideLong = ""
sideShort = ""
sideList = ["Right", "Left"]

for side in sideList:
    sideLong = side
    sideShort = side[0]

    jointRemapList =[
        ["Char_Hips", "Root_M"],
        ["Char_" + sideLong + "Finger1", "IndexFinger1_" + sideShort],
        ["Char_" + sideLong + "Finger2", "IndexFinger2_" + sideShort]
        ]

    for jointPair in jointRemapList:

        print sideLong, sideShort, jointPair
Sign up to request clarification or add additional context in comments.

1 Comment

I agree this is the better solution, though it's going to be a bit slower than the str.format approach of Jared's.
3

So the issue is that you are creating the strings when jointRemapList is created, and updates to sideLong and sideShort are not going to be reflected in those strings because strings are immuatable. Try using string formatting instead.

from itertools import product

joint_remap_list = [
    ["Char_Hips", "Root_M"],
    ["Char_{side}Finger1", "IndexFinger1_{abbrev}"],
    ["Char_{side}Finger2", "IndexFinger2_{abbrev}"]
]

sides = ["Left", "Right"]

for side, pair in product(sides, joint_remap_list):
    abbrev = side[0]
    formatted = [s.format(side=side, abbrev=abbrev) for s in pair]
    print side, abbrev, formatted

1 Comment

consider defining the formatted list on a separate line. fmtd_joint_pair = [s.format(long=sideLong, short=sideShort) for s in jointPair]; print sideLong, sideShort, fmtd_join_pair feels nicer to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.