[Tutor] A list in list problem

dave s pythontut at pusspaws.net
Mon Aug 21 11:59:01 CEST 2006


Help :)

I have been playing with this for several hours & am totally stuck !

I am happy with the following ...

>>> a=[1,2,3]
>>> b=[5,6,7]
>>> a
[1, 2, 3]
>>> b
[5, 6, 7]
>>> g=[]
>>> g
[]
>>> g.append(a)
>>> g
[[1, 2, 3]]
>>> g.append(b)
>>> g
[[1, 2, 3], [5, 6, 7]]
>>>

So when I needed to make a list of a list in the following code I thought no 
problem ...


   def CSV_Lines(self, csv, from_, to):
        """Returns a list of cleaned up lines from csv 'from_' line 
number  'to' line number"""
        
        clean_line = clean_csv = []
        for string in range(from_, to):
            split_string = csv[string].split(',')
            split_string = split_string[1:-1]  # Strip the LHS column + 
the /n'
            if split_string[0] == '' : continue  # Skip empty lines
            print '##########################'
            print 'split_string ', split_string
            for string in split_string:
                if len(string) > 0: clean_line.append(string[1:-1])
            print 'clean_line ',clean_line
            clean_csv.append(clean_line)
            print 'clean_csv ',clean_csv
            clean_line = []

But I get clean_csv trouble  ...

ubuntu at ubuntu:~/python_develop/unison/PxQxAudit/main$ ./crosscheck.py
##########################
split_string  ['"temp1"', '"wow a variable"', '', '', '']
clean_line  ['temp1', 'wow a variable']
clean_csv  ['temp1', 'wow a variable', [...]]
##########################
split_string  ['"temp2"', '', '', '', '']
clean_line  ['temp2']
clean_csv  ['temp1', 'wow a variable', [...], ['temp2']]
ubuntu at ubuntu:~/python_develop/unison/PxQxAudit/main$

ie clean_csv ends up as ['temp1', 'wow a variable', [...], ['temp2']] instead 
of [[temp1, wow a variable], [temp2]]

Please help

Dave
















More information about the Tutor mailing list