You don't even need to use str.join() to remove leading or trailing split 
elements. split() has a *maxsplit *argument which limits the number of 
splits. rsplit() does the same as split but it starts at the end (right) of 
the string.

Also it's a good practice not to use python builtin types/methods as 
variable names (list is a builtin type)

objs = cmds.ls('*_set*')
for obj in objs:
    # Splits at "_" twice, starting from the end of the string and working 
backwards
    # And returns the rest (unsplit portion) of the string
    new_fn = obj.rsplit('_', 2)[0]
    print new_fn

On Tuesday, October 30, 2012 6:28:16 AM UTC-7, Anthony Tan wrote:
>
> if you just wanted to slice off bits from the end, you change the 
> increment in the [ ], so to remove the last two [:-2]
>
> more generally on slice notation (basically its [start_position : 
> end_position : step] but the examples below will explain it out in 
> longhand.. ):
> http://bergbom.blogspot.com.au/2011/04/python-slice-notation.html 
>
> http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation
>
>
> On Tuesday, October 30, 2012 8:23:21 PM UTC+8, Daz wrote:
>>
>> Heya
>>
>> Wow now that was really easy...
>>
>> So this is sorted and I can move on :D 
>>
>> Last tiny question tho... how can I remove multiple parts of the name? 
>> Just for the future...
>>
>> list = cmds.ls('*_set*')
>> for objects in list:
>>   new_fn = "_".join(objects.split("_")[:-1-2]) 
>> print new_fn
>>
>> ?? somehow it dont work :s neither if I put -1,-2 or in separate []....
>>
>> Thanks again !
>>
>>
>>
>> On Tuesday, 30 October 2012 12:19:06 UTC, matthew evans wrote:
>>>
>>> if you simply want to "remove" the last part of the name try:
>>>
>>> list = cmds.ls('*_set*')
>>> for objects in list:
>>>     new_fn = "_".join(object.split("_")[:-1])
>>>     print new_fn
>>>
>>> On Tue, Oct 30, 2012 at 12:07 PM, Daz <[email protected]> wrote:
>>>
>>>> Heya
>>>>
>>>> Aaa right !  Thats explains a lot !
>>>>
>>>> So I can reformat them and put back together any way I want. Now that 
>>>> sort of works but I hit a wall if some of the names has xx_xx_xx_xx and 
>>>> some are xx_xx_xx. Then I get error....
>>>>
>>>> Here is basic script
>>>> list = cmds.ls('*_set*')
>>>>
>>>> for objects in list:
>>>> t = objects.split('_')
>>>> new_fn = "{0}_{1}_{2}_{3}".format(t[-0],t[-2],t[-3],t[-4])
>>>> print new_fn
>>>>
>>>> and here is error
>>>>
>>>> # Error: list index out of range
>>>> # Traceback (most recent call last):
>>>> # File "<maya console>", line 5, in <module>
>>>> # IndexError: list index out of range # 
>>>>
>>>> I guess its because names have different length...
>>>>
>>>> I try using Try/except:pass but then it just loop again and add 1st 
>>>> part to the end so that wont work whh...
>>>>
>>>> Any hints how to just subtract/delete the last part of name?
>>>>
>>>>
>>>> On Tuesday, 30 October 2012 11:49:01 UTC, Anthony Tan wrote:
>>>>>
>>>>> With your format() call, you need a string format specifier to tell it 
>>>>> how to use the arguments you've supplied it..for example, in the working 
>>>>> script one, the format specifier is the string "{0}_JY_{1}"
>>>>>
>>>>> If you did a straight print on this, you'd get literally "{0}_JY_{1}", 
>>>>> but since you're invoking format() against it, it'll go through and 
>>>>> replace 
>>>>> the {0} with the first argument in format() and {1} with the 2nd 
>>>>> argument, 
>>>>> and so on and so on. The { } are markers for where format() should be 
>>>>> inserting values. Your loop currently is calling format(t[0], t[1]) 
>>>>> against 
>>>>> a string that doesn't contain any of the { }'s so it's not doing 
>>>>> anything, 
>>>>> and the result is that new_fn is the same as objects 
>>>>>
>>>>> Is this more what you're after?
>>>>>
>>>>> list = cmds.ls('*_set*') 
>>>>>
>>>>> for objects in list:
>>>>>
>>>>>     # t = cmds.ls(objects)[0].split('_')[-1]
>>>>>
>>>>>     # no need to call cmds.ls again to select anything, you've
>>>>>
>>>>>     # already got the name with the first cmds.ls('*_set*') call
>>>>>
>>>>>     t = objects.split('_')[-1]
>>>>>
>>>>>
>>>>>     # new_fn = objects.format( t[0], t[1] )
>>>>>     # apply a new prefix to the setXYZ suffix
>>>>>
>>>>>     new_fn = "aNewPrefix_{0}".format(t)
>>>>>
>>>>>
>>>>>
>>>>> On Tuesday, October 30, 2012 7:09:07 PM UTC+8, Daz wrote:
>>>>>>
>>>>>> Heya
>>>>>>
>>>>>> I'm battling with a part of my script... I want to cmds.ls certain 
>>>>>> items and then remove part of their name. After that I want the 
>>>>>> resulting 
>>>>>> name to be used for next part of the script. The wall I hit is with 
>>>>>> .format 
>>>>>> string... I got an working example but somehow it makes no sense to me 
>>>>>> whh... anyway have a look, if any1 could push me to the right direction 
>>>>>> that would be great ! I should be able to pick it up after hehe :)
>>>>>>
>>>>>>
>>>>>> Here is example of working script:
>>>>>>
>>>>>> fn = "LN0001_07272010_3.dat".split('_')
>>>>>> new_fn = '{0}_JY_{1}'.format(fn[0], fn[1])
>>>>>> print new_fn
>>>>>>
>>>>>> but he uses premade name... 
>>>>>>
>>>>>> and here is my try
>>>>>>
>>>>>> list = cmds.ls('*_set*')
>>>>>> for objects in list:
>>>>>>       t = cmds.ls(objects)[0].split('_')[-1]
>>>>>>       new_fn = objects.format( t[0], t[1] )
>>>>>>       print new_fn
>>>>>>
>>>>>> I just want to be able to remove _set and use the new name to select 
>>>>>> nodes and run next script...
>>>>>>
>>>>>> Thanks in advance! :)
>>>>>>
>>>>> -- 
>>>> view archives: http://groups.google.com/group/python_inside_maya
>>>> change your subscription settings: 
>>>> http://groups.google.com/group/python_inside_maya/subscribe
>>>>
>>>
>>>

-- 
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: 
http://groups.google.com/group/python_inside_maya/subscribe

Reply via email to