[Tutor] Help return a pattern from list
anand.shashwat at gmail.com
Mon Jul 5 20:29:55 CEST 2010
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 =
> 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 =
> >>> [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) == '.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) == '.mp3']
> For smaller case string is good, for obscure patter, you can try regex
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor