Statement (un)equality

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

    Statement (un)equality

    castle:/home/adam>python
    Python 2.3 (#3, Aug 4 2003, 16:43:33)
    [GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> [[y,x] for x,y in [1,2],[3,4]][/color][/color][/color]
    [[2, 1], [4, 3]][color=blue][color=green][color=darkred]
    >>> map(lambda x,y: [y,x], [1,2],[3,4])[/color][/color][/color]
    [[3, 1], [4, 2]][color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
    Why there is a difference? How to make the same thing like in map statement?
    Regards
    Adam Przybyla
  • Erik Max Francis

    #2
    Re: Statement (un)equality

    Adam Przybyla wrote:
    [color=blue][color=green][color=darkred]
    > >>> [[y,x] for x,y in [1,2],[3,4]][/color][/color]
    > [[2, 1], [4, 3]][color=green][color=darkred]
    > >>> map(lambda x,y: [y,x], [1,2],[3,4])[/color][/color]
    > [[3, 1], [4, 2]][color=green][color=darkred]
    > >>>[/color][/color]
    > Why there is a difference? How to make the same thing like in map
    > statement?[/color]

    Because the same things aren't happening here, despite their outward
    similarity. For the second case to be equivalent to the first, you
    really meant:
    [color=blue][color=green][color=darkred]
    >>> map(lambda x: [x[1], x[0]], [[1, 2], [3, 4]])[/color][/color][/color]
    [[2, 1], [4, 3]]

    You can pass multiple sequences to map, and that interleaves the
    results:
    [color=blue][color=green][color=darkred]
    >>> map(lambda x, y, z: (x, y, z), [1, 2], [3, 4], [5, 6])[/color][/color][/color]
    [(1, 3, 5), (2, 4, 6)]

    --
    __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    \__/ Come not between the dragon and his wrath.
    -- King Lear (Act I, Scene I)

    Comment

    • Jonas Galvez

      #3
      Re: Statement (un)equality

      > [Adam Przybyla][color=blue][color=green][color=darkred]
      > >>> [[y,x] for x,y in [1,2],[3,4]][/color][/color]
      > [[2, 1], [4, 3]][color=green][color=darkred]
      > >>> map(lambda x,y: [y,x], [1,2],[3,4])[/color][/color]
      > [[3, 1], [4, 2]][/color]
      [color=blue][color=green][color=darkred]
      >>> def test(a,b):[/color][/color][/color]
      print "a:", a, "b:", b
      return [b,a][color=blue][color=green][color=darkred]
      >>> map(test, [1,2], [3,4])[/color][/color][/color]
      a: 1 b: 3
      a: 2 b: 4
      [[3, 1], [4, 2]][color=blue][color=green][color=darkred]
      >>> [test(a, b) for a, b in [1,2], [3,4]][/color][/color][/color]
      a: 1 b: 2
      a: 3 b: 4
      [[2, 1], [4, 3]][color=blue][color=green][color=darkred]
      >>> [test(a, b) for a, b in zip([1,2], [3,4])][/color][/color][/color]
      a: 1 b: 3
      a: 2 b: 4
      [[3, 1], [4, 2]]



      Jonas





      Comment

      • James Henderson

        #4
        Re: Statement (un)equality

        On Friday 20 February 2004 10:31 am, Adam Przybyla wrote:[color=blue]
        > castle:/home/adam>python
        > Python 2.3 (#3, Aug 4 2003, 16:43:33)
        > [GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
        > Type "help", "copyright" , "credits" or "license" for more information.
        >[color=green][color=darkred]
        > >>> [[y,x] for x,y in [1,2],[3,4]][/color][/color]
        >
        > [[2, 1], [4, 3]]
        >[color=green][color=darkred]
        > >>> map(lambda x,y: [y,x], [1,2],[3,4])[/color][/color]
        >
        > [[3, 1], [4, 2]]
        >
        > Why there is a difference?[/color]

        Hi,

        [[y,x] for x,y in [1,2],[3,4]]

        is just the same as:

        [[y,x] for (x,y) in ([1,2],[3,4])]

        The list comprehension regards "[1,2],[3,4]" as a single argument (a tuple)
        and "x,y" is also a tuple. "x,y" is assigned first [1,2] and then [3,4].

        In the map statement "[1,2],[3,4]" are two different arguments. map's
        signature allows for any number of iterables to be passed after the first
        argument.
        [color=blue]
        > How to make the same thing like in map
        > statement?[/color]

        zip() gives you the sequence of pairs you want to pass: (zip() is very
        similar to map with None as the first arguemnt. The difference is how they
        handle sequences of unequal length.)
        [color=blue][color=green][color=darkred]
        >>> zip([1,2],[3,4])[/color][/color][/color]
        [(1, 3), (2, 4)]

        so:

        [[y,x] for x,y in zip([1,2],[3,4])]

        will match the behaviour of the map statement.
        [color=blue]
        > Regards
        > Adam Przybyla[/color]

        James
        --
        James Henderson, Logical Progression Ltd.

        Download MailManager for free. MailManager is designed to solve the problems companies have as the volumes of email they receive increase such as making sure email goes to the right person, making sure it is answered on time, ensuring information in email boxes is shared within th


        Comment

        Working...