473,408 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

A simple list question.

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.

Dec 22 '05 #1
4 1944
KraftDiner wrote:
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
Dec 22 '05 #2
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...

Dec 22 '05 #3
KraftDiner wrote:
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
Dec 22 '05 #4
KraftDiner wrote:
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
Dec 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: george young | last post by:
For each run of my app, I have a known set of (<100) wafer names. Names are sometimes simply integers, sometimes a short string, and sometimes a short string followed by an integer, e.g.: 5, 6,...
3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
1
by: Joseph Barron | last post by:
Here is a SIMPLE problem that I'm trying to solve. It works in Netscape 6.2, but IE6 gives ""No such interface supported." Below are page1.htm and page2.htm . In page1.htm, there are two...
10
by: JustSomeGuy | last post by:
I have a few classes... class a : std::list<baseclass> { int keya; }; class b : std::list<a> { int keyb;
5
by: Stephanie_Stowe | last post by:
Hi. I am trying to get used to AS.NET. I have been doing ASP classic for years, and am now in a position to do ASP.NET. I am in the stumbling around until I get my bearings phase. I hope you will...
13
by: Saber | last post by:
I did a lot of searches and read something about datagrids. But I couldn't find the answer of my simple question, how can I show only my desired columns of a table? for example I wrote this sql...
6
by: Robert W. | last post by:
I have a situation where I have pairs of strings that I want to put into an ArrayList and then later pull out of it. To keep things simple I tried this to put them in: arrayList.Add(new object...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
11
by: Olaf Krumnow | last post by:
Hi, I have a requirement that, I thought, was very simple, but in fact turned out as impossible for me. I read some posts here and the language specs from MS, but couldn't get a solution. ...
2
by: nicholas08 | last post by:
I am new to programming and trying to work on a simple console app. I am making a simple Library Management system where the user/admin can manage members and item. Attaching the menu so it's clear...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.