newbie q

E

Egor Bolonev

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()
 
S

Stephen Thorne

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
 
E

Egor Bolonev

"Stephen Thorne" <[email protected]> Ñообщил/Ñообщила в новоÑÑ‚ÑÑ…
Ñледующее:
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
 
S

Stephen Thorne

"Stephen Thorne" <[email protected]> Ñообщил/Ñообщила в новоÑÑ‚ÑÑ…
Ñледующее:
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.
 
W

Will Stuyvesant

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']
 
S

Steve Holden

Egor said:
"Stephen Thorne" <[email protected]> Ñообщил/Ñообщила в новоÑÑ‚ÑÑ…
Ñледующее:
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
 
B

Bengt Richter

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
>>> 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
 
C

Christos TZOTZIOY Georgiou

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):
 
S

Steve Holden

Bengt said:
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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top