You are not logged in.
Hey,
I've done PHP before and I like arrays, they're nice and to me, very easy to work with. Python, veeerrryyyy different. I'm slowly getting the hang of it but I want to do some changing here, I'm using a dictionary at the moment but in another section of code, it would be a lot easier if it were a list, and object would be easier than how I have it at the moment but a list is preferred.
Now I could rewrite it to use a list (I think), but it would look insanely messy due to the need to define the variable as a list, eg.
var = []
var[0] = []
var[1] = []
In PHP terms, I'd like an array of arrays. Here is the code that makes a dict and is working, is there a neat and tidy way to rewrite this to create a list or something more managable?
def _loadServers(self, file):
# load servers
try:
f = open('servers.txt')
buf = f.read()
f.close()
except:
dPrint("error fetching server list")
if buf is not None:
lines = buf.split("n")
if lines[:-1] is None:
lines.pop( len(lines)-1 )
servers = {}
for server in lines:
i = len(servers)
servers[i] = {}
servers[i][0] = server.split(' ')[0]
servers[i][1] = server.split(' ')[1]
return servers
return False
and in case you want to see what I'm trying to do, the problem is later on, I'd like to remove a set from the "array", if it were a list I could use pop() which is better than setting the base key to None which is causing errors when in a different section of the code I loop over the array looking for subarrays.
def _removeServer(self, host):
for s in self.servers:
server = self.servers[ s ]
if server[1] == host:
self.servers[ s ] = None
return True
self._saveServers('servers.txt')
return False
That sounds really confusing actually, hope someone understands my dribble...
Thanks
Offline
you dont have to assign a list to each member of the original list.
define your list...
supalist = []
and when you wish to add to it,
suparlist.append(object)
where object is another list, variable, class, whatever you want.
iphitus
def _loadServers(self, file):
# load servers
try:
f = open('servers.txt')
buf = f.read()
f.close()
except:
dPrint("error fetching server list")
if buf is not None:
lines = buf.split("n")
if lines[:-1] is None:
lines.pop( len(lines)-1 )
servers = []
for server in lines:
splitserver = server.split()
servers.append([splitserver[0],splitserver[1])
return servers
return False
Offline
I never thought of doing it that way, and it's not messy! Thanks
Offline
try help(list) from your python interpreter
Offline
Aye, I generally avoid defining each element of a list by hand. I much prefer to init the list as empt and just append to it, much neater .
Writing stories for a machine.
Offline