[Tutor] Reading directory

Blake Winton bwinton at latte.ca
Wed Jan 21 17:38:18 EST 2004


> family_01.jpg
> family_02.jpg
> family_03.jpg
> family_03.jpg 
> 
> test_01.jpg
> test_02.jpg 
> 
> It would optimilly select just.
> family
> test 

Okay, I'm going to assume that you have got a list of filenames
like so:
>>> x = ['family_01.jpg', 'family_02.jpg', 'family_03.jpg',
'family_03.jpg', 'test_01.jpg', 'test_02.jpg' ]
If you don't, perhaps you can take some time trying to figure
out how to get that.  Or perhaps you can wait, and someone else
will tell you how to do it.

To go from that to ['family', 'test'], you can use the following
code:
>>> ignore = [y.update({z.split('_')[0]:1}) for z in x]
>>> y
{'test': 1, 'family': 1}
>>> w = y.keys()
>>> w
['test', 'family']

And voila!  :)

Play around with bits of it, to see if you can figure out what
it's doing there.  If you can't quite get it, feel free to email
me, and ask me questions about it.

Later,
Blake.




More information about the Tutor mailing list