Hello, On Wed, May 22, 2013 at 5:49 PM, Stuart Tozer <stuto...@gmail.com> wrote: > Hi everyone. > > I'm stuck on a problem while developing a small tool for Maya. Basically, I > have a folder with some filenames in it which have been returned by > os.listdir(). These filenames look something like... > > > apple_d.jpg > apple_si.jpg > apple_sg.jpg > box_d.jpg > box_si.jpg > pumpkin_d.jpg > > > Right now, I'm using os.listdir to get the filenames, and then each filename > becomes a selectable option in an option menu in maya. The code is... > > objects = os.listdir(dir) > > > > > > for object in objects: > cmds.menuItem(label = object, parent = "objectMenu") > > > > My problem is that I don't want duplicates of the same object in the menu > and I also want to truncate everything past and including the underscore. So > for eg, if my filenames are the same as the example above, I want to change > the list so I have this instead... > > apple > box > pumpkin > > > I have tried splitting the filenames, but so far haven't had any luck. > > for object in objects: > sorted(set(object.split('_', 1)[0])) > cmds.menuItem(label = object, parent = "objectMenu")
You are almost there. Something like this should work: >>> labels = ['apple_d.jpg','apple_si.jpg','apple_sg.jpg','box_si.jpg'] Now, create a list of only the names (upto _): >>> labels_processed=[ ] >>> for label in labels: ... labels_processed.append(label.split('_')[0]) ... Remove the duplicates: >>> set(labels_processed) set(['box', 'apple']) If you have any doubts about this, please ask. Best, Amit. -- http://echorand.me _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor