newbie q

Discussion in 'Python' started by Egor Bolonev, Jan 13, 2005.

  1. Egor Bolonev

    Egor Bolonev Guest

    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()
    Egor Bolonev, Jan 13, 2005
    #1
    1. Advertising

  2. 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
    Stephen Thorne, Jan 13, 2005
    #2
    1. Advertising

  3. Egor Bolonev

    Egor Bolonev Guest

    "Stephen Thorne" <> Ñообщил/Ñообщила в новоÑÑ‚ÑÑ…
    Ñледующее: news:...
    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
    Egor Bolonev, Jan 13, 2005
    #3
  4. On Thu, 13 Jan 2005 17:05:39 +1000, Egor Bolonev <> wrote:
    >
    > "Stephen Thorne" <> Ñообщил/Ñообщила в новоÑÑ‚ÑÑ…
    > Ñледующее: news:...
    > 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.
    Stephen Thorne, Jan 13, 2005
    #4
  5. 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
    Will Stuyvesant, Jan 13, 2005
    #5
  6. Egor Bolonev

    Steve Holden Guest

    Egor Bolonev wrote:

    >
    > "Stephen Thorne" <> Ñообщил/Ñообщила в новоÑÑ‚ÑÑ…
    > Ñледующее: news:...
    > 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
    Steve Holden, Jan 13, 2005
    #6
  7. 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
    Bengt Richter, Jan 14, 2005
    #7
  8. 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 RFC1958)
    I really should keep that in mind when talking with people, actually...
    Christos TZOTZIOY Georgiou, Jan 14, 2005
    #8
  9. Egor Bolonev

    Steve Holden Guest

    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
    Steve Holden, Jan 14, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Id0x
    Replies:
    4
    Views:
    1,169
    Erik Max Francis
    Jul 21, 2003
  2. JohnE

    newbie with newbie questions

    JohnE, Aug 17, 2009, in forum: ASP .Net
    Replies:
    3
    Views:
    482
    Gregory A. Beamer
    Aug 17, 2009
  3. Jerry C.
    Replies:
    8
    Views:
    206
    Uri Guttman
    Nov 23, 2003
  4. Kruno Saho
    Replies:
    0
    Views:
    125
    Kruno Saho
    Apr 7, 2013
  5. Dave Angel
    Replies:
    0
    Views:
    110
    Dave Angel
    Apr 7, 2013
Loading...

Share This Page