Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Referring to a list

Reply
Thread Tools

Referring to a list

 
 
Sean Berry
Guest
Posts: n/a
 
      06-25-2004
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.


 
Reply With Quote
 
 
 
 
Russell Blau
Guest
Posts: n/a
 
      06-25-2004
"Sean Berry" <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      06-25-2004
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" <(E-Mail Removed)> 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.
>
>



 
Reply With Quote
 
Dave Kuhlman
Guest
Posts: n/a
 
      06-25-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Referring to a control on another page Greg Smith ASP .Net 1 12-30-2004 07:42 PM
Referring to a User Control up one level in the Directory Structure Dave ASP .Net 3 12-07-2004 03:54 PM
Referring to a frame's content from another frame Microsoft ASP .Net 3 10-04-2004 06:35 AM
how can I get tthe referring page? buran ASP .Net 2 03-01-2004 01:28 PM
Referring URL\VB George ASP .Net 3 02-09-2004 10:25 PM



Advertisments