A simple list question.

K

KraftDiner

Is there a cleaner way to implement this code?

if len(self.listOfObjects) == 0:
self.listOfObjects.append(self.currentObject)
elif:
self.listOfObjects[self.currentSlice] = self.currentObject

listOfObjects is a list of lists....
If the listOfObjects is [] then I have to use append to add the first
list
otherwise I can access each list using the index into the list.
 
L

Larry Bates

KraftDiner said:
Is there a cleaner way to implement this code?

if len(self.listOfObjects) == 0:
self.listOfObjects.append(self.currentObject)
elif:
self.listOfObjects[self.currentSlice] = self.currentObject

listOfObjects is a list of lists....
If the listOfObjects is [] then I have to use append to add the first
list
otherwise I can access each list using the index into the list.
It appears you are trying to do two different things in one if
statement and it is unclear where self.currentSlice comes from.
You also need a condition on your elif. Perhaps more explanation
is needed (at least for me to help).

-Larry Bates
 
K

KraftDiner

I am trying to implement a two dimensional array.
mylist = [[a,b,c],[d,e,f,c],[g,h,i]]

So the array is of length 3 here...
So how do I initialize this array and then set each object?
At some point in my code I know there will be 3 lists in the list.
So how do I initialize this list such that I can access them
as elements... like
mylist[1] = [c,f,g]
in random order...
 
K

Kent Johnson

KraftDiner said:
I am trying to implement a two dimensional array.
mylist = [[a,b,c],[d,e,f,c],[g,h,i]]

So the array is of length 3 here...
So how do I initialize this array and then set each object?
At some point in my code I know there will be 3 lists in the list.
So how do I initialize this list such that I can access them
as elements... like
mylist[1] = [c,f,g]
in random order...

If you can separate the code into a list builder and a list client, maybe the builder can
just use append and the client can use indexing.

Kent
 
T

Tomasz Lisowski

KraftDiner said:
I am trying to implement a two dimensional array.
mylist = [[a,b,c],[d,e,f,c],[g,h,i]]

So the array is of length 3 here...
So how do I initialize this array and then set each object?
At some point in my code I know there will be 3 lists in the list.
So how do I initialize this list such that I can access them
as elements... like
mylist[1] = [c,f,g]
in random order...
Why not just say:

mylist = [[], [], []]

since you know, that this array will have a length of 3.

Then you can safely use append. I am not quite sure, though, what you
mean by accessing the elements in random order. What's the idea behind
having mylist[1] = [c,f,g], when the elements c, f, and g are apread
across all three internal lists, and at various position within these
lists (not necessarily 1, as the mylist index suggests).

Tomasz Lisowski
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,274
Messages
2,571,368
Members
48,060
Latest member
JerrodSimc

Latest Threads

Top