Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > newbie q

Reply
Thread Tools

newbie q

 
 
Egor Bolonev
Guest
Posts: n/a
 
      01-13-2005
how to get rid of 'for' operator in the code?


import os, os.path

def _test():
src = 'C:\\Documents and Settings\\\\My Documents\\My Music\\'

for i in [x for x in os.listdir(src) if os.path.isfile(os.path.join(src,
x)) and len(x.split('.')) > 1 and x.split('.')[-1].lower() == 'm3u']:
os.remove(os.path.join(src, i))

if __name__ == '__main__':
_test()

 
Reply With Quote
 
 
 
 
Stephen Thorne
Guest
Posts: n/a
 
      01-13-2005
On Thu, 13 Jan 2005 15:55:10 +1000, Egor Bolonev <> wrote:
> how to get rid of 'for' operator in the code?
>
> import os, os.path
>
> def _test():
> src = 'C:\\Documents and Settings\\Егор\\My Documents\\My Music\\'
>
> for i in [x for x in os.listdir(src) if os.path.isfile(os.path.join(src,
> x)) and len(x.split('.')) > 1 and x.split('.')[-1].lower() == 'm3u']:
> os.remove(os.path.join(src, i))
>
> if __name__ == '__main__':
> _test()


import glob
for x in glob.glob("*.m3u"):
os.remove(x)

Regards,
Stephen Thorne
 
Reply With Quote
 
 
 
 
Egor Bolonev
Guest
Posts: n/a
 
      01-13-2005

"Stephen Thorne" <> сообщил/сообщила в новостях
следующее: news:mailman.611.1105598828.22381.python-...
On Thu, 13 Jan 2005 15:55:10 +1000, Egor Bolonev <> wrote:
> how to get rid of 'for' operator in the code?
>
> import os, os.path
>
> def _test():
> src = 'C:\\Documents and Settings\\Егор\\My Documents\\My Music\\'
>
> for i in [x for x in os.listdir(src) if
> os.path.isfile(os.path.join(src,
> x)) and len(x.split('.')) > 1 and x.split('.')[-1].lower() == 'm3u']:
> os.remove(os.path.join(src, i))
>
> if __name__ == '__main__':
> _test()


import glob
for x in glob.glob("*.m3u"):
os.remove(x)

i want to wipe out 'for x in []: f(x)' using map, lambda, reduce, filter and
List
Comprehensions [x for x in []]
just don't get how to add string to all elements of list

 
Reply With Quote
 
Stephen Thorne
Guest
Posts: n/a
 
      01-13-2005
On Thu, 13 Jan 2005 17:05:39 +1000, Egor Bolonev <> wrote:
>
> "Stephen Thorne" <> сообщил/сообщила в новостях
> следующее: news:mailman.611.1105598828.22381.python-...
> On Thu, 13 Jan 2005 15:55:10 +1000, Egor Bolonev <> wrote:
> > how to get rid of 'for' operator in the code?
> >
> > import os, os.path
> >
> > def _test():
> > src = 'C:\\Documents and Settings\\Егор\\My Documents\\My Music\\'
> >
> > for i in [x for x in os.listdir(src) if
> > os.path.isfile(os.path.join(src,
> > x)) and len(x.split('.')) > 1 and x.split('.')[-1].lower() == 'm3u']:
> > os.remove(os.path.join(src, i))
> >
> > if __name__ == '__main__':
> > _test()

>
> import glob
> for x in glob.glob("*.m3u"):
> os.remove(x)
>
> i want to wipe out 'for x in []: f(x)' using map, lambda, reduce, filter and
> List
> Comprehensions [x for x in []]
> just don't get how to add string to all elements of list


Here's a few ways,

map(os.remove, glob.glob("*.m3u"))
[os.remove(x) for x in glob.glob("*.m3u")]

[os.remove(x) for x in os.listdir(src) if
os.path.isfile(os.path.join(src, x))
and len(x.split('.')) > 1
and x.split('.')[-1].lower() == 'm3u']

def myfilter(x):
return os.path.isfile(os.path.join(src, x)) and len(x.split('.')) >
1 and x.split('.')[-1].lower() == 'm3u'
map(os.remove, filter(myfilter, os.listdir(src)))

Regards,
Stephen Thorne.
 
Reply With Quote
 
Will Stuyvesant
Guest
Posts: n/a
 
      01-13-2005
I understand you want to do it in an applicative programming style?
Not recommended in general. But here goes:

..# c.l.p. question:
..# "using map, lambda, reduce, filter and List Comprehensions [x for
..# x in []] just don't get how to add string to all elements of list"
..
..##
..# A function that returns a function, taking a function as argument.
..def allapply(fn):
.. def f(seq): return map(fn, seq)
.. return f
..
..def add_string_to_element(stringval):
.. def f(x): return x + stringval
.. return f
..
..add_string_to_all = allapply(add_string_to_element('mystring'))
..
..print add_string_to_all(['d:', 'c:\windows\\','something/'])

this outputs:
['d:mystring', 'c:\\windows\\mystring', 'something/mystring']
--
look Ma, no for and no lambda!
-- Will Stuyvesant

 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      01-13-2005
Egor Bolonev wrote:

>
> "Stephen Thorne" <> сообщил/сообщила в новостях
> следующее: news:mailman.611.1105598828.22381.python-...
> On Thu, 13 Jan 2005 15:55:10 +1000, Egor Bolonev <> wrote:
>
>> how to get rid of 'for' operator in the code?
>>
>> import os, os.path
>>
>> def _test():
>> src = 'C:\\Documents and Settings\\Егор\\My Documents\\My Music\\'
>>
>> for i in [x for x in os.listdir(src) if
>> os.path.isfile(os.path.join(src,
>> x)) and len(x.split('.')) > 1 and x.split('.')[-1].lower() == 'm3u']:
>> os.remove(os.path.join(src, i))
>>
>> if __name__ == '__main__':
>> _test()

>
>
> import glob
> for x in glob.glob("*.m3u"):
> os.remove(x)
>
> i want to wipe out 'for x in []: f(x)' using map, lambda, reduce, filter
> and List
> Comprehensions [x for x in []]
> just don't get how to add string to all elements of list
>


Any statement of the form

for i in [x for x in something]:

can be rewritten as

for i in something:

Note that this doesn't mean you never want to iterate over a list
comprehension. It's the easiest way, for example, to iterate over the
first item of each list in a list of lists:

for i in [x[0] for x in something]:

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119

 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      01-14-2005
On Thu, 13 Jan 2005 09:16:40 -0500, Steve Holden <> wrote:
[...]
>
>Any statement of the form
>
> for i in [x for x in something]:
>
>can be rewritten as
>
> for i in something:
>
>Note that this doesn't mean you never want to iterate over a list
>comprehension. It's the easiest way, for example, to iterate over the
>first item of each list in a list of lists:
>
> for i in [x[0] for x in something]:
>

As I'm sure you know, with 2.4's generator expressions you
don't have to build the temporary list.
Which could be important if 'something'
is (or generates) a huge sequence.

for i in (x[0] for x in something):

>>> something = ([x] for x in xrange(10,20))
>>> something

<generator object at 0x02EF176C>
>>> list(something)

[[10], [11], [12], [13], [14], [15], [16], [17], [18], [19]]
>>> for i in (x[0] for x in something): print i,

...

oops, that list() used it up

>>> something = [[x] for x in xrange(10,20)]
>>> for i in (x[0] for x in something): print i,

...
10 11 12 13 14 15 16 17 18 19

Really nice.

Regards,
Bengt Richter
 
Reply With Quote
 
Christos TZOTZIOY Georgiou
Guest
Posts: n/a
 
      01-14-2005
On Fri, 14 Jan 2005 00:08:09 GMT, rumours say that (Bengt
Richter) might have written:

>As I'm sure you know, with 2.4's generator expressions you
>don't have to build the temporary list.
>Which could be important if 'something'
>is (or generates) a huge sequence.
>
> for i in (x[0] for x in something):


and for some functional fun:

from itertools import imap
from operator import itemgetter
for i in imap(itemgetter(0), something):
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC195
I really should keep that in mind when talking with people, actually...
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      01-14-2005
Bengt Richter wrote:

> On Thu, 13 Jan 2005 09:16:40 -0500, Steve Holden <> wrote:
> [...]
>
>>Any statement of the form
>>
>> for i in [x for x in something]:
>>
>>can be rewritten as
>>
>> for i in something:
>>
>>Note that this doesn't mean you never want to iterate over a list
>>comprehension. It's the easiest way, for example, to iterate over the
>>first item of each list in a list of lists:
>>
>> for i in [x[0] for x in something]:
>>

>
> As I'm sure you know, with 2.4's generator expressions you
> don't have to build the temporary list.
> Which could be important if 'something'
> is (or generates) a huge sequence.
>
> for i in (x[0] for x in something):
>

Yes. While I haven't yet done any more than play with generator
sequences I do really feel that more of "the best of Icon" has arrived
in Python with this new addition.

> >>> something = ([x] for x in xrange(10,20))
> >>> something

> <generator object at 0x02EF176C>
> >>> list(something)

> [[10], [11], [12], [13], [14], [15], [16], [17], [18], [19]]
> >>> for i in (x[0] for x in something): print i,

> ...
>
> oops, that list() used it up
>
> >>> something = [[x] for x in xrange(10,20)]
> >>> for i in (x[0] for x in something): print i,

> ...
> 10 11 12 13 14 15 16 17 18 19
>
> Really nice.
>

I quite agree. It's particularly useful for infinite sequences

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie with newbie questions JohnE ASP .Net 3 08-17-2009 10:10 PM
VONAGE Newbie w/newbie question New_kid@nowhere.new VOIP 0 08-11-2007 01:40 PM
another newbie question from another newbie.... Lee UK VOIP 4 05-17-2005 04:10 PM
newbie: cisco vlan newbie question No Spam Cisco 3 06-07-2004 10:02 AM
Newbie! I'm a newbie! What's wrong with this program? Id0x Python 4 07-20-2003 11:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57