execution order in list/generator expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bonono@gmail.com

    #1

    execution order in list/generator expression

    Hi,

    I am wondering how this is evaluated.

    a=(x for x in [1,2,3,4])
    p=[4,5]

    c=[x for x in p if x in list(a)]

    c is []

    but if I expand a first, like a = list(a)

    c is [4]

    So it seems that the "if" part don't get expanded ?

  • Devan L

    #2
    Re: execution order in list/generator expression

    bonono@gmail.co m wrote:[color=blue]
    > Hi,
    >
    > I am wondering how this is evaluated.
    >
    > a=(x for x in [1,2,3,4])
    > p=[4,5]
    >
    > c=[x for x in p if x in list(a)]
    >
    > c is []
    >
    > but if I expand a first, like a = list(a)
    >
    > c is [4]
    >
    > So it seems that the "if" part don't get expanded ?[/color]

    Well, for every element in p it recalculates list(a). Since the
    generator is exhausted after making a list from it, it gives you
    nothing afterwards. So after it checks the first element, it's
    equivalent to [x for x in p if x in []].

    Comment

    • Robert Kern

      #3
      Re: execution order in list/generator expression

      bonono@gmail.co m wrote:[color=blue]
      > Hi,
      >
      > I am wondering how this is evaluated.
      >
      > a=(x for x in [1,2,3,4])
      > p=[4,5]
      >
      > c=[x for x in p if x in list(a)]
      >
      > c is [][/color]

      No it isn't.

      In [1]: a=(x for x in [1,2,3,4])

      In [2]: p=[4,5]

      In [3]: c=[x for x in p if x in list(a)]

      In [4]: c
      Out[4]: [4]

      I'm willing to bet that you used up the 'a' iterator before you ran that
      list comprehension, though.

      In [5]: c=[x for x in p if x in list(a)]

      In [6]: c
      Out[6]: []

      Note that "x in list(a)" gets executed on each iteration, but the
      iterator is used up on the first time.

      In [7]: a=(x for x in [1,2,3,4])

      In [8]: p = [4, 5, 2, 3]

      In [9]: c=[x for x in p if x in list(a)]

      In [10]: c
      Out[10]: [4]

      --
      Robert Kern
      rkern@ucsd.edu

      "In the fields of hell where the grass grows high
      Are the graves of dreams allowed to die."
      -- Richard Harter

      Comment

      • bonono@gmail.com

        #4
        Re: execution order in list/generator expression

        Ah, no wonder. I test with p=[5,4].

        thanks. so basically, I still need to expand it first given this
        behaviour.

        Robert Kern wrote:[color=blue]
        > bonono@gmail.co m wrote:[color=green]
        > > Hi,
        > >
        > > I am wondering how this is evaluated.
        > >
        > > a=(x for x in [1,2,3,4])
        > > p=[4,5]
        > >
        > > c=[x for x in p if x in list(a)]
        > >
        > > c is [][/color]
        >
        > No it isn't.
        >
        > In [1]: a=(x for x in [1,2,3,4])
        >
        > In [2]: p=[4,5]
        >
        > In [3]: c=[x for x in p if x in list(a)]
        >
        > In [4]: c
        > Out[4]: [4]
        >
        > I'm willing to bet that you used up the 'a' iterator before you ran that
        > list comprehension, though.
        >
        > In [5]: c=[x for x in p if x in list(a)]
        >
        > In [6]: c
        > Out[6]: []
        >
        > Note that "x in list(a)" gets executed on each iteration, but the
        > iterator is used up on the first time.
        >
        > In [7]: a=(x for x in [1,2,3,4])
        >
        > In [8]: p = [4, 5, 2, 3]
        >
        > In [9]: c=[x for x in p if x in list(a)]
        >
        > In [10]: c
        > Out[10]: [4]
        >
        > --
        > Robert Kern
        > rkern@ucsd.edu
        >
        > "In the fields of hell where the grass grows high
        > Are the graves of dreams allowed to die."
        > -- Richard Harter[/color]

        Comment

        Working...