[Tutor] Reading directory

Blake Winton bwinton at latte.ca
Wed Jan 21 17:47:50 EST 2004


> >>> x = ['family_01.jpg', 'family_02.jpg', 'family_03.jpg',
> 'family_03.jpg', 'test_01.jpg', 'test_02.jpg' ]
> 
> To go from that to ['family', 'test'], you can use the following
> code:

Hah!  That'll larn me to go writing code without reading the
documentation first...

If you've installed Python 2.3, you can also write 
>>> import sets
>>> y = sets.Set( [z.split('_')[0] for z in x] )
>>> y
Set(['test', 'family'])

which is a whole lot less code, and hopefully a little more
understandable.

Later,
Blake.




More information about the Tutor mailing list