[x for x in <> while <>]?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • urikaluzhny

    [x for x in <> while <>]?

    It seems that I rather frequently need a list or iterator of the form
    [x for x in <while <>]
    And there is no one like this.
    May be there is another short way to write it (not as a loop). Is
    there?
    Thanks
  • Terry Reedy

    #2
    Re: [x for x in &lt;&gt; while &lt;&gt;]?


    "urikaluzhn y" <ukaluzhny@nds. comwrote in message
    news:f8229614-a000-450e-85eb-825e6c1386cf@w7 g2000hsa.google groups.com...
    | It seems that I rather frequently need a list or iterator of the form
    | [x for x in <while <>]

    I can think of two ways to interpret that.

    | And there is no one like this.
    | May be there is another short way to write it (not as a loop). Is
    | there?

    Using loops to write an example of what you mean would make the above
    clearer.

    tjr



    Comment

    • Bruno Desthuilliers

      #3
      Re: [x for x in &lt;&gt; while &lt;&gt;]?

      urikaluzhny a écrit :
      It seems that I rather frequently need a list or iterator of the form
      [x for x in <while <>]
      And there is no one like this.
      May be there is another short way to write it (not as a loop). Is
      there?
      The answer is very probably in the itertools module.

      Comment

      • Paul Rubin

        #4
        Re: [x for x in &lt;&gt; while &lt;&gt;]?

        urikaluzhny <ukaluzhny@nds. comwrites:
        It seems that I rather frequently need a list or iterator of the form
        [x for x in <while <>]
        And there is no one like this.
        May be there is another short way to write it (not as a loop). Is there?
        itertools.takew hile(condition, seq)

        Comment

        • urikaluzhny

          #5
          Re: ?

          On May 15, 10:06 am, "Terry Reedy" <tjre...@udel.e duwrote:
          "urikaluzhn y" <ukaluz...@nds. comwrote in message
          >
          news:f8229614-a000-450e-85eb-825e6c1386cf@w7 g2000hsa.google groups.com...
          | It seems that I rather frequently need a list or iterator of the form
          | [x for x in <while <>]
          >
          I can think of two ways to interpret that.
          I mean like [x for x in <Aif <B>], only that it breaks the loop when
          the expression <Bis false.

          Comment

          • Geoffrey Clements

            #6
            Re: ?

            "urikaluzhn y" <ukaluzhny@nds. comwrote in message
            news:51e0f25d-474a-450a-ad00-92f70c893c6c@m4 4g2000hsc.googl egroups.com...
            On May 15, 10:06 am, "Terry Reedy" <tjre...@udel.e duwrote:
            "urikaluzhn y" <ukaluz...@nds. comwrote in message
            >
            news:f8229614-a000-450e-85eb-825e6c1386cf@w7 g2000hsa.google groups.com...
            | It seems that I rather frequently need a list or iterator of the form
            | [x for x in <while <>]
            >
            I can think of two ways to interpret that.
            >I mean like [x for x in <Aif <B>], only that it breaks the loop when
            >the expression <Bis false.
            def gen(a):
            for x in a:
            if B: break
            yield x

            a_gen = gen(A)

            # now iterate over a_gen

            --
            Geoff


            Comment

            • afrobeard

              #7
              Re: ?

              The following proposed solution is not intended to be a solution, it
              goes completely against the zen of python. [Type import this into the
              python command interpreter]

              I brought it down to two lines:-

              l = range(6)
              [1 if b!=4 else l.__delslice__( 0,len(l)) for b in l][:-1]

              itertools would still be a better approach in my opinion.

              Just because I'm curious to know, can anyone bring it shorter[even if
              its cryptic] than this without invoking any Python Library.

              P.S. Once again I would not recommend using this as Explicit is better
              than Implicit
              P.P.S. It is strongly undesirable for us humans to use anything
              starting with __ :)


              On May 15, 5:10 pm, "Geoffrey Clements"
              <geoffrey.cleme nt...@SPAMbaesy stems.comwrote:
              "urikaluzhn y" <ukaluz...@nds. comwrote in message
              >
              news:51e0f25d-474a-450a-ad00-92f70c893c6c@m4 4g2000hsc.googl egroups.com...
              On May 15, 10:06 am, "Terry Reedy" <tjre...@udel.e duwrote:
              >
              "urikaluzhn y" <ukaluz...@nds. comwrote in message
              >
              news:f8229614-a000-450e-85eb-825e6c1386cf@w7 g2000hsa.google groups.com...
              | It seems that I rather frequently need a list or iterator of the form
              | [x for x in <while <>]
              >
              I can think of two ways to interpret that.
              I mean like [x for x in <Aif <B>], only that it breaks the loop when
              the expression <Bis false.
              >
              def gen(a):
                  for x in a:
                      if B: break
                      yield x
              >
              a_gen = gen(A)
              >
              # now iterate over a_gen
              >
              --
              Geoff

              Comment

              • castironpi

                #8
                Re: ?

                On May 15, 6:07 pm, afrobeard <afrobe...@gmai l.comwrote:
                The following proposed solution is not intended to be a solution, it
                goes completely against the zen of python. [Type import this into the
                python command interpreter]
                >
                I brought it down to two lines:-
                >
                l = range(6)
                [1 if b!=4 else l.__delslice__( 0,len(l)) for b in l][:-1]
                >
                itertools would still be a better approach in my opinion.
                >
                Just because I'm curious to know, can anyone bring it shorter[even if
                its cryptic] than this without invoking any Python Library.
                >
                P.S. Once again I would not recommend using this as Explicit is better
                than Implicit
                P.P.S. It is strongly undesirable for us humans to use anything
                starting with __ :)
                >
                On May 15, 5:10 pm, "Geoffrey Clements"
                >
                >
                >
                <geoffrey.cleme nt...@SPAMbaesy stems.comwrote:
                "urikaluzhn y" <ukaluz...@nds. comwrote in message
                >
                news:51e0f25d-474a-450a-ad00-92f70c893c6c@m4 4g2000hsc.googl egroups.com...
                On May 15, 10:06 am, "Terry Reedy" <tjre...@udel.e duwrote:
                >
                "urikaluzhn y" <ukaluz...@nds. comwrote in message
                >
                >news:f822961 4-a000-450e-85eb-825e6c1386cf@w7 g2000hsa.google groups.com....
                | It seems that I rather frequently need a list or iterator of the form
                | [x for x in <while <>]
                >
                I can think of two ways to interpret that.
                >I mean like [x for x in <Aif <B>], only that it breaks the loop when
                >the expression <Bis false.
                >
                def gen(a):
                    for x in a:
                        if B: break
                        yield x
                >
                a_gen = gen(A)
                >
                # now iterate over a_gen
                >
                --
                Geoff- Hide quoted text -
                >
                - Show quoted text -
                In your original, you have:
                l = range(6)
                [1 if b!=4 else l.__delslice__( 0,len(l)) for b in l][:-1]
                You may be hyperextending the use of '..if..else..', which is one of
                my fears regarding 'with x as y'. "l.__delslice__ (0,len(l))" is not
                an expression.

                Comment

                • afrobeard

                  #9
                  Re: ?

                  According to http://en.wikipedia.org/wiki/Expression_(programming)

                  "An expression in a programming language is a combination of values,
                  variables, operators, and functions that are interpreted (evaluated)
                  according to the particular rules of precedence and of association for
                  a particular programming language, which computes and then produces
                  (returns, in a stateful environment) another value."

                  l.__delslice__( 0,len(l)) is an expression because it returns None
                  [which also happens to be a value] in this case.

                  On May 16, 4:23 am, castironpi <castiro...@gma il.comwrote:
                  On May 15, 6:07 pm, afrobeard <afrobe...@gmai l.comwrote:
                  >
                  >
                  >
                  The following proposed solution is not intended to be a solution, it
                  goes completely against the zen of python. [Type import this into the
                  python command interpreter]
                  >
                  I brought it down to two lines:-
                  >
                  l = range(6)
                  [1 if b!=4 else l.__delslice__( 0,len(l)) for b in l][:-1]
                  >
                  itertools would still be a better approach in my opinion.
                  >
                  Just because I'm curious to know, can anyone bring it shorter[even if
                  its cryptic] than this without invoking any Python Library.
                  >
                  P.S. Once again I would not recommend using this as Explicit is better
                  than Implicit
                  P.P.S. It is strongly undesirable for us humans to use anything
                  starting with __ :)
                  >
                  On May 15, 5:10 pm, "Geoffrey Clements"
                  >
                  <geoffrey.cleme nt...@SPAMbaesy stems.comwrote:
                  "urikaluzhn y" <ukaluz...@nds. comwrote in message
                  >
                  >news:51e0f25 d-474a-450a-ad00-92f70c893c6c@m4 4g2000hsc.googl egroups.com....
                  On May 15, 10:06 am, "Terry Reedy" <tjre...@udel.e duwrote:
                  >
                  "urikaluzhn y" <ukaluz...@nds. comwrote in message
                  >
                  news:f8229614-a000-450e-85eb-825e6c1386cf@w7 g2000hsa.google groups.com....
                  | It seems that I rather frequently need a list or iterator of the form
                  | [x for x in <while <>]
                  >
                  I can think of two ways to interpret that.
                  I mean like [x for x in <Aif <B>], only that it breaks the loop when
                  the expression <Bis false.
                  >
                  def gen(a):
                      for x in a:
                          if B: break
                          yield x
                  >
                  a_gen = gen(A)
                  >
                  # now iterate over a_gen
                  >
                  --
                  Geoff- Hide quoted text -
                  >
                  - Show quoted text -
                  >
                  In your original, you have:
                  >
                  l = range(6)
                  [1 if b!=4 else l.__delslice__( 0,len(l)) for b in l][:-1]
                  >
                  You may be hyperextending the use of '..if..else..', which is one of
                  my fears regarding 'with x as y'.  "l.__delslice__ (0,len(l))" is not
                  an expression.

                  Comment

                  • afrobeard

                    #10
                    Re: ?

                    l.__delslice__( 0,len(l)) is an expression as it returns None which is
                    a value


                    On May 16, 4:23 am, castironpi <castiro...@gma il.comwrote:
                    On May 15, 6:07 pm, afrobeard <afrobe...@gmai l.comwrote:
                    >
                    >
                    >
                    The following proposed solution is not intended to be a solution, it
                    goes completely against the zen of python. [Type import this into the
                    python command interpreter]
                    >
                    I brought it down to two lines:-
                    >
                    l = range(6)
                    [1 if b!=4 else l.__delslice__( 0,len(l)) for b in l][:-1]
                    >
                    itertools would still be a better approach in my opinion.
                    >
                    Just because I'm curious to know, can anyone bring it shorter[even if
                    its cryptic] than this without invoking any Python Library.
                    >
                    P.S. Once again I would not recommend using this as Explicit is better
                    than Implicit
                    P.P.S. It is strongly undesirable for us humans to use anything
                    starting with __ :)
                    >
                    On May 15, 5:10 pm, "Geoffrey Clements"
                    >
                    <geoffrey.cleme nt...@SPAMbaesy stems.comwrote:
                    "urikaluzhn y" <ukaluz...@nds. comwrote in message
                    >
                    >news:51e0f25 d-474a-450a-ad00-92f70c893c6c@m4 4g2000hsc.googl egroups.com....
                    On May 15, 10:06 am, "Terry Reedy" <tjre...@udel.e duwrote:
                    >
                    "urikaluzhn y" <ukaluz...@nds. comwrote in message
                    >
                    news:f8229614-a000-450e-85eb-825e6c1386cf@w7 g2000hsa.google groups.com....
                    | It seems that I rather frequently need a list or iterator of the form
                    | [x for x in <while <>]
                    >
                    I can think of two ways to interpret that.
                    I mean like [x for x in <Aif <B>], only that it breaks the loop when
                    the expression <Bis false.
                    >
                    def gen(a):
                        for x in a:
                            if B: break
                            yield x
                    >
                    a_gen = gen(A)
                    >
                    # now iterate over a_gen
                    >
                    --
                    Geoff- Hide quoted text -
                    >
                    - Show quoted text -
                    >
                    In your original, you have:
                    >
                    l = range(6)
                    [1 if b!=4 else l.__delslice__( 0,len(l)) for b in l][:-1]
                    >
                    You may be hyperextending the use of '..if..else..', which is one of
                    my fears regarding 'with x as y'.  "l.__delslice__ (0,len(l))" is not
                    an expression.

                    Comment

                    • castironpi

                      #11
                      Re: ?

                      On May 15, 6:52 pm, afrobeard <afrobe...@gmai l.comwrote:
                      l.__delslice__( 0,len(l)) is an expression as it returns None which is
                      a value
                      >
                      On May 16, 4:23 am, castironpi <castiro...@gma il.comwrote:
                      >
                      >
                      >
                      On May 15, 6:07 pm, afrobeard <afrobe...@gmai l.comwrote:
                      >
                      The following proposed solution is not intended to be a solution, it
                      goes completely against the zen of python. [Type import this into the
                      python command interpreter]
                      >
                      I brought it down to two lines:-
                      >
                      l = range(6)
                      [1 if b!=4 else l.__delslice__( 0,len(l)) for b in l][:-1]
                      >
                      itertools would still be a better approach in my opinion.
                      >
                      Just because I'm curious to know, can anyone bring it shorter[even if
                      its cryptic] than this without invoking any Python Library.
                      >
                      P.S. Once again I would not recommend using this as Explicit is better
                      than Implicit
                      P.P.S. It is strongly undesirable for us humans to use anything
                      starting with __ :)
                      >
                      On May 15, 5:10 pm, "Geoffrey Clements"
                      >
                      <geoffrey.cleme nt...@SPAMbaesy stems.comwrote:
                      "urikaluzhn y" <ukaluz...@nds. comwrote in message
                      >
                      news:51e0f25d-474a-450a-ad00-92f70c893c6c@m4 4g2000hsc.googl egroups.com...
                      On May 15, 10:06 am, "Terry Reedy" <tjre...@udel.e duwrote:
                      >
                      "urikaluzhn y" <ukaluz...@nds. comwrote in message
                      >
                      >news:f822961 4-a000-450e-85eb-825e6c1386cf@w7 g2000hsa.google groups.com...
                      | It seems that I rather frequently need a list or iterator of theform
                      | [x for x in <while <>]
                      >
                      I can think of two ways to interpret that.
                      >I mean like [x for x in <Aif <B>], only that it breaks the loopwhen
                      >the expression <Bis false.
                      >
                      def gen(a):
                          for x in a:
                              if B: break
                              yield x
                      >
                      a_gen = gen(A)
                      >
                      # now iterate over a_gen
                      >
                      --
                      Geoff- Hide quoted text -
                      >
                      - Show quoted text -
                      >
                      In your original, you have:
                      >
                      l = range(6)
                      [1 if b!=4 else l.__delslice__( 0,len(l)) for b in l][:-1]
                      >
                      You may be hyperextending the use of '..if..else..', which is one of
                      my fears regarding 'with x as y'.  "l.__delslice__ (0,len(l))" is not
                      an expression.- Hide quoted text -
                      >
                      - Show quoted text -
                      Functional programming is really important to a former professor of
                      me. I like to say that None returns as a value. I think you can call
                      functional "evaluation al".

                      Comment

                      • Alexandr N Zamaraev

                        #12
                        Re: ?

                        urikaluzhny wrote:
                        >| It seems that I rather frequently need a list or iterator of the form
                        >| [x for x in <while <>]
                        >I can think of two ways to interpret that.
                        I mean like [x for x in <Aif <B>], only that it breaks the loop when
                        the expression <Bis false.
                        How do you plan to modify B during iteration?
                        May be
                        [x for x in itertools.takew hile(<B>, <A>)]
                        when <Bfunction accept element <Aand return True or False


                        Comment

                        • Ruediger

                          #13
                          Re: [x for x in &lt;&gt; while &lt;&gt;]?

                          urikaluzhny wrote:
                          It seems that I rather frequently need a list or iterator of the form
                          [x for x in <while <>]
                          And there is no one like this.
                          May be there is another short way to write it (not as a loop). Is
                          there?
                          Thanks
                          I usually have the same problem and i came up with an solution like that:

                          from operator import ne
                          def test(iterable, value, op=ne):
                          _n = iter(iterable). next
                          while True:
                          _x = _n()
                          if op(_x, value):
                          yield _x
                          else:
                          raise StopIteration

                          l = range(6)
                          print [x for x in test(l, 4)]

                          r@linux:~/tmppython test18.py
                          [0, 1, 2, 3]

                          Comment

                          • Paul Hankin

                            #14
                            Re: ?

                            On May 17, 11:08 am, Ruediger <larud...@freen et.dewrote:
                            urikaluzhny wrote:
                            It seems that I rather frequently need a list or iterator of the form
                            [x for x in <while <>]
                            And there is no one like this.
                            May be there is another short way to write it (not as a loop). Is
                            there?
                            Thanks
                            >
                            I usually have the same problem and i came up with an solution like that:
                            >
                            from operator import ne
                            def test(iterable, value, op=ne):
                                _n = iter(iterable). next
                                while True:
                                    _x = _n()
                                    if op(_x, value):
                                        yield _x
                                    else:
                                        raise StopIteration
                            This is better written using takewhile...
                            itertools.takew hile(lambda x: x != value, iterable)

                            But if you really need to reinvent the wheel, perhaps this is simpler?

                            def test(iterable, value, op=operator.ne) :
                            for x in iterable:
                            if not op(x, value):
                            return
                            yield x

                            --
                            Paul Hankin

                            Comment

                            • Ruediger

                              #15
                              Re: ?

                              Paul Hankin wrote:

                              This is better written using takewhile...
                              itertools.takew hile(lambda x: x != value, iterable)
                              >
                              But if you really need to reinvent the wheel, perhaps this is simpler?
                              >
                              def test(iterable, value, op=operator.ne) :
                              for x in iterable:
                              if not op(x, value):
                              return
                              yield x

                              yes you are right it is. However as i mentioned in my post i came up with an
                              solution 'like' that. In fact my original code was to complex to post.
                              While simplifying it, i've overseen the obvious solution.

                              For special cases where you need to do more complex tests, the best solution
                              is IMHO to hide it in an generator function like above.



                              Comment

                              Working...