Referring to a list

Discussion in 'Python' started by Sean Berry, Jun 25, 2004.

  1. Sean Berry

    Sean Berry Guest

    I have a function like this:

    final_list = []

    def doSomething():
    for item in starting_list = []:
    Do all sorts of stuff... about 150 lines of processing.
    Lots of conditional statements for appending to the final_list.
    So I have lots of final_list.append( results ) statements.

    I wanted to single out a few cases and have
    them append to a different list.

    The way I would "normally" do something like this would be to have
    all of the .append() statements in another function like this

    def addToList( listName, otherInfo )
    do something with otherInfo.
    then listName.append( results )

    But, in this case, by the time I get to this point there
    are too many variables to pass (something like 60).

    So, what I want to do is use some kind of name reference
    for the list I want to use.

    EXAMPLE:
    final_list = []
    secondary_list = []

    def DoSomething():
    if (condition is met):
    list_i_am_referring_to = final_list
    else
    list_i_am_referring_to = secondary_list

    then I can do everything using the list_i_am_referring_to.

    Is this possible???

    Sorry about my poor, and lengthy explanation.
    Thanks for any help.
     
    Sean Berry, Jun 25, 2004
    #1
    1. Advertising

  2. Sean Berry

    Russell Blau Guest

    "Sean Berry" <> wrote in message
    news:1a0Dc.13333$rh.4819@okepread02...
    > So, what I want to do is use some kind of name reference
    > for the list I want to use.
    >
    > EXAMPLE:
    > final_list = []
    > secondary_list = []
    >
    > def DoSomething():
    > if (condition is met):
    > list_i_am_referring_to = final_list
    > else
    > list_i_am_referring_to = secondary_list
    >
    > then I can do everything using the list_i_am_referring_to.
    >
    > Is this possible???


    Does this help?

    >>> odd_list = []
    >>> even_list = []
    >>> def which_list(x):

    if x % 2 == 0:
    return even_list
    else:
    return odd_list

    >>> for i in range(20):

    which_list(i).append(i)


    >>> odd_list

    [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
    >>> even_list

    [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]


    --
    I don't actually read my hotmail account, but you can replace hotmail with
    excite if you really want to reach me.
     
    Russell Blau, Jun 25, 2004
    #2
    1. Advertising

  3. Sean Berry

    Larry Bates Guest

    Absolutely. Everything in Python is a pointer.

    x=final_list

    is a pointer to final_list

    x=final_list_other

    changes what x points to.

    Larry Bates
    Syscon, Inc.

    "Sean Berry" <> wrote in message
    news:1a0Dc.13333$rh.4819@okepread02...
    > I have a function like this:
    >
    > final_list = []
    >
    > def doSomething():
    > for item in starting_list = []:
    > Do all sorts of stuff... about 150 lines of processing.
    > Lots of conditional statements for appending to the final_list.
    > So I have lots of final_list.append( results ) statements.
    >
    > I wanted to single out a few cases and have
    > them append to a different list.
    >
    > The way I would "normally" do something like this would be to have
    > all of the .append() statements in another function like this
    >
    > def addToList( listName, otherInfo )
    > do something with otherInfo.
    > then listName.append( results )
    >
    > But, in this case, by the time I get to this point there
    > are too many variables to pass (something like 60).
    >
    > So, what I want to do is use some kind of name reference
    > for the list I want to use.
    >
    > EXAMPLE:
    > final_list = []
    > secondary_list = []
    >
    > def DoSomething():
    > if (condition is met):
    > list_i_am_referring_to = final_list
    > else
    > list_i_am_referring_to = secondary_list
    >
    > then I can do everything using the list_i_am_referring_to.
    >
    > Is this possible???
    >
    > Sorry about my poor, and lengthy explanation.
    > Thanks for any help.
    >
    >
     
    Larry Bates, Jun 25, 2004
    #3
  4. Sean Berry

    Dave Kuhlman Guest

    Russell Blau wrote:

    [snip]

    >
    > Does this help?
    >
    >>>> odd_list = []
    >>>> even_list = []
    >>>> def which_list(x):

    > if x % 2 == 0:
    > return even_list
    > else:
    > return odd_list
    >
    >>>> for i in range(20):

    > which_list(i).append(i)
    >
    >
    >>>> odd_list

    > [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
    >>>> even_list

    > [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
    >
    >


    Basically, what this and other replies to your question are
    telling you is that lists in Python are first-class objects. To
    be a first-class object means that you can (1) stuff it into a
    data structure (for example, a dictionary or another list), pass
    it in to a function, and (3) return it as the value of a function.

    Also remember that functions in Python are first class, so you do
    something like this:

    def do_job(arg, listarg, even_func, odd_func):
    if arg % 2 == 0:
    even_func(arg, listarg)
    else:
    odd_func(arg, listarg)

    That makes doing delegation in Python easy and natural. OK. OK.
    It's not natural, but it's about as natural as something as weird
    as computer programming is going to get.

    Dave


    --
    Dave Kuhlman
    http://www.rexx.com/~dkuhlman
     
    Dave Kuhlman, Jun 26, 2004
    #4
    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. George

    Referring URL\VB

    George, Feb 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    7,481
    George
    Feb 9, 2004
  2. buran

    how can I get tthe referring page?

    buran, Mar 1, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    3,504
    buran
    Mar 1, 2004
  3. Microsoft
    Replies:
    3
    Views:
    701
    Hugo Wetterberg
    Oct 4, 2004
  4. Dave
    Replies:
    3
    Views:
    938
  5. Greg Smith

    Referring to a control on another page

    Greg Smith, Dec 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    451
    John Saunders
    Dec 30, 2004
Loading...

Share This Page