Re: [Tutor] how to alter list content

2005-10-13 Thread w chun
combining the best of both worlds of nick using a faster list comp and brett using os.path.splitext():>>> files = ['DSC1.JPG', 'DSC2.JPG', 'DSC3.JPG']>>> newFiles = [os.path.splitext
(f)[0] for f in files]>>> print newFiles['DSC1', 'DSC2', 'DSC3']cheers,-- wesley- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"Core Python Programming", Prentice Hall, (c)2006,2001
http://corepython.comwesley.j.chun :: wescpy-at-gmail.comcyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to alter list content

2005-10-13 Thread Brett Hoerner
> i create a list of all JPG files with:
> >>> list = glob.glob('*.JPG')
> the content of 'list' is now:
> >>> print list
> ['DSC1.JPG', 'DSC2.JPG', 'DSC3.JPG']
>
> but what i want is this type of list:
> ['DSC1', 'DSC2', 'DSC3']

I would make use of os.path.splitext, which returns a tuple, where [0]
is the path and filename, and [1] is the extension.

In [1]: import os
In [2]: mylist = ['DSC1.JPG', 'DSC2.JPG', 'DSC3.JPG']
In [3]: newlist = list()
In [4]: for x in mylist:
   : newlist.append(os.path.splitext(x)[0])
In [5]: newlist
Out[6]: ['DSC1', 'DSC2', 'DSC3']

Also, don't assign things to list, list() is a buildin function in
Python, you don't want to say list = x, because then you can't get to
the buitin list().

Brett
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to alter list content

2005-10-13 Thread Nick Lunt
Hi marc,

> 
> i create a list of all JPG files with:
> >>> list = glob.glob('*.JPG')
> the content of 'list' is now:
> >>> print list
> ['DSC1.JPG', 'DSC2.JPG', 'DSC3.JPG']
> 
> but what i want is this type of list:
> ['DSC1', 'DSC2', 'DSC3']
> i.e. the names w/o the file extension.
> 
> what's the easiest way of doing this?
> 
> marc

You need the split() method.
ie 
>> l = ['DSC1.JPG', 'DSC2.JPG', 'DSC3.JPG']
>> print [i.split('.')[0] for i in l] 

Hth,
Nick .

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to alter list content

2005-10-13 Thread Marc Buehler
hi.

i create a list of all JPG files with:
>>> list = glob.glob('*.JPG')
the content of 'list' is now:
>>> print list
['DSC1.JPG', 'DSC2.JPG', 'DSC3.JPG']

but what i want is this type of list:
['DSC1', 'DSC2', 'DSC3']
i.e. the names w/o the file extension.

what's the easiest way of doing this?

marc

---
The apocalyptic vision of a criminally insane charismatic cult leader 

   http://www.marcbuehler.net




__ 
Yahoo! Music Unlimited 
Access over 1 million songs. Try it free.
http://music.yahoo.com/unlimited/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor