You are not logged in.

#1 2005-07-20 12:40:55

Cam
Member
From: Brisbane, Aus
Registered: 2004-12-21
Posts: 658
Website

python - got dict, would rather object or list

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 smile

Offline

#2 2005-07-20 12:45:05

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: python - got dict, would rather object or list

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

#3 2005-07-20 13:30:22

Cam
Member
From: Brisbane, Aus
Registered: 2004-12-21
Posts: 658
Website

Re: python - got dict, would rather object or list

I never thought of doing it that way, and it's not messy! big_smile Thanks smile

Offline

#4 2005-07-20 17:26:44

cmp
Member
Registered: 2005-01-03
Posts: 350

Re: python - got dict, would rather object or list

try help(list) from your python interpreter

Offline

#5 2005-07-21 04:14:21

dust
Member
Registered: 2005-06-04
Posts: 152
Website

Re: python - got dict, would rather object or list

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 smile .


Writing stories for a machine.

Offline

Board footer

Powered by FluxBB