On Mon, Jul 5, 2010 at 11:58 PM, Shashwat Anand <anand.shashwat at gmail.com>wrote:
>>> On Mon, Jul 5, 2010 at 11:24 PM, Vineeth Rakesh <vineethrakesh at gmail.com>wrote:
>>> Hello all,
>>>> Can some one help me to return a special pattern from a list.
>>>> say list =
>> ["something1.mp3","something2.mp3","something4.pdf","something5.odt"]
>>>> One suggestion. Don't name a list as list. Use l or List or any other
> variable name. list is one of the syntax in python.
>>>>>> now say I just need to return the files with .mp3 extension. How to go
>> about doing this?
>>>> >>> list =
> ["something1.mp3","something2.mp3","something4.pdf","something5.odt"]
> >>> [i for i in list if i[-4:] == '.mp3']
> ['something1.mp3', 'something2.mp3']
>> or may be ,
> >>> [i for i in list if os.path.splitext(i)[0] == '.mp3'] # If you want to
> deal with file extentions, that is.
>
Oops, sorry for the typo.
It'll be ,
>>> [i for i in list if os.path.splitext(i)[1] == '.mp3']
> For smaller case string is good, for obscure patter, you can try regex
> module.
>> ~l0nwlf
>-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100705/7b0aa56b/attachment.html>