I have a list...

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

    I have a list...

    Hi, All!

    say, i have a function:

    def f(*b):
    print b
    return

    then i do:
    f(3,4,5)
    (3, 4, 5)

    but i have list f=(3,4,5)
    f(l)
    ((3, 4, 5),)

    how can i call f function to result
    f(???(b))
    (3, 4, 5)

    Thanks!



  • Albert Hofkamp

    #2
    Re: I have a list...

    On Tue, 1 Jul 2003 11:39:15 +0400, Damir Hakimov <[email protected] u> wrote:[color=blue]
    > Hi, All!
    >
    > say, i have a function:
    >
    > def f(*b):
    > print b
    > return
    >
    > then i do:
    > f(3,4,5)
    > (3, 4, 5)
    >
    > but i have list f=(3,4,5)
    > f(l)
    > ((3, 4, 5),)
    >
    > how can i call f function to result
    > f(???(b))
    > (3, 4, 5)[/color]

    You mean apply(f,(3,4,5) ) ?


    Albert
    --
    Unlike popular belief, the .doc format is not an open publically available format.

    Comment

    • Dialtone

      #3
      Re: I have a list...

      "Damir Hakimov" <[email protected] u> writes:
      [color=blue]
      > say, i have a function:
      >
      > def f(*b):
      > print b
      > return
      >
      > then i do:
      > f(3,4,5)
      > (3, 4, 5)[/color]

      This is not a list but a tuple.
      [color=blue]
      > but i have list f=(3,4,5)
      > f(l)
      > ((3, 4, 5),)[/color]

      The standard way to represent a tuple with one element is to put a coma
      after that element like ("donald",)
      [color=blue]
      > how can i call f function to result
      > f(???(b))
      > (3, 4, 5)[/color]

      If you want this you should use a list which has square brackets [].

      But the arguments passed with *b are incapsuleted into a tuple so you should
      print something like this:
      [color=blue][color=green][color=darkred]
      >>> def f(*b):[/color][/color][/color]
      .... print b[0]
      [color=blue][color=green][color=darkred]
      >>> f([1,2,3])[/color][/color][/color]
      [1, 2, 3]



      --
      try: troll.uses(Brai n)
      except TypeError, data:
      troll.plonk()
      Linux User #310274, Debian Sid Proud User

      Comment

      • user@domain.invalid

        #4
        Re: I have a list...

        Damir Hakimov wrote:[color=blue]
        > Hi, All!
        >
        > say, i have a function:
        >
        > def f(*b):
        > print b
        > return
        >
        > then i do:
        > f(3,4,5)
        > (3, 4, 5)
        >
        > but i have list f=(3,4,5)
        > f(l)
        > ((3, 4, 5),)
        >
        > how can i call f function to result
        > f(???(b))
        > (3, 4, 5)
        >
        > Thanks!
        >
        >
        >[/color]
        You can use the keyword 'type' to check the type of your arguments
        and return the appropriate 'format' according to the their types

        Regards

        Salvatore


        Comment

        • Duncan Booth

          #5
          Re: I have a list...

          "Damir Hakimov" <[email protected] u> wrote in
          news:2003070111 3915.226ae9bd.a [email protected]:
          [color=blue]
          > Hi, All!
          >
          > say, i have a function:
          >
          > def f(*b):
          > print b
          > return
          >
          > then i do:
          > f(3,4,5)
          > (3, 4, 5)
          >
          > but i have list f=(3,4,5)
          > f(l)
          > ((3, 4, 5),)
          >
          > how can i call f function to result
          > f(???(b))
          > (3, 4, 5)
          >[/color]
          I'm not sure any of the other responses actually answered the question,
          which I think was meant to be, given a tuple l=3,4,5 how do you pass that
          tuple to the function f so that b simply gets the tuple. The answer is that
          you try:
          [color=blue][color=green][color=darkred]
          >>> f(*l)[/color][/color][/color]
          (3,4,5)

          If that doesn't work, then you upgrade to a more recent version of Python.
          If you (or your users) really can't upgrade you should use 'apply'.

          --
          Duncan Booth [email protected] k
          int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
          "\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?

          Comment

          • Aahz

            #6
            Re: I have a list...

            In article <20030701113915 .226ae9bd.agg@a stranet.ru>,
            Damir Hakimov <[email protected] u> wrote:[color=blue]
            >
            > [...][/color]

            ....it never will be missed.
            --
            Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

            Apologies to G&S and everyone reading this.

            Comment

            • Bengt Richter

              #7
              Re: I have a list...

              On Tue, 1 Jul 2003 11:39:15 +0400, "Damir Hakimov" <[email protected] u> wrote:
              [color=blue]
              >Hi, All!
              >
              >say, i have a function:
              >
              >def f(*b):
              > print b
              > return
              >
              >then i do:
              >f(3,4,5)
              >(3, 4, 5)
              >
              >but i have list f=(3,4,5)
              >f(l)
              >((3, 4, 5),)
              >
              >how can i call f function to result
              >f(???(b))
              >(3, 4, 5)
              >[/color]
              Is this what you are looking for? :
              [color=blue][color=green][color=darkred]
              >>> def f(*b):[/color][/color][/color]
              ... print b
              ...[color=blue][color=green][color=darkred]
              >>> tup = (1,2,3)
              >>> f(tup)[/color][/color][/color]
              ((1, 2, 3),)

              tup was single arg, but:
              [color=blue][color=green][color=darkred]
              >>> f(*tup)[/color][/color][/color]
              (1, 2, 3)

              tup got unpacked to make args
              [color=blue][color=green][color=darkred]
              >>> L = [4,5,6]
              >>> f(L)[/color][/color][/color]
              ([4, 5, 6],)

              L was single arg, but:
              [color=blue][color=green][color=darkred]
              >>> f(*L)[/color][/color][/color]
              (4, 5, 6)

              L got unpacked similarly, but note that args become tuple b, not a list.

              Regards,
              Bengt Richter

              Comment

              Working...