I'm learning Python to use wMaya - a 3D graphics app.
While reverse engineering some code, I found a thread here which explains "list comprehension". The code I'm analyzing uses that, so I attempted to deconstruct it, but I can't reproduce the same results using a for loop.
I have a list:
print tmpJnts [nt.Joint(u'joint1'), nt.Joint(u'joint2'), nt.Joint(u'joint3'), nt.Joint(u'joint4'), nt.Joint(u'joint5')]
I was shown how to convert it to a string using list comprehension:
jntStringList = ' '.join([j.name() for j in tmpJnts]) print jntStringList joint1 joint2 joint3 joint4 joint5
I'm curious how to use a for loop to accomplish this. I've tried a few approaches, but no luck:
for j in tmpJnts: print j.name() print (' '.join([j.name()] ) ) print (' '.join(j.name() ) ) # Without brackets, the string is treated as a list: j o i n t 5 jntStringList2.append(' '.join([j.name()])) #Append will create a new list item for each iteration jntStringList3 += (' ' + j.name() ) # joint1 joint1 j o i n t 1 joint2 joint2 j o i n t 2 joint3 joint3 j o i n t 3 joint4 joint4 j o i n t 4 joint5 joint5 j o i n t 5 print jntStringList2 [u'joint1', u'joint2', u'joint3', u'joint4', u'joint5'] print jntStringList3 [u' ', u'j', u'o', u'i', u'n', u't', u'1', u' ', u'j',...]
Can anyone explain how to do use a loop to convert a list to a string in this case?