Reading File Into 2D List

alex.hanga at gmail.com alex.hanga at gmail.com
Tue Jul 9 11:07:47 EDT 2013


 > Replace te **start line with something like:
> 
> 
> 
>             object_data.append([])
> 
>             i += 1
> 
> 
> 
> This assumes a few missing lines, which must have been there or you 
> 
> would have already had runtime errors.  For example, you'll need i=0 
> 
> before the loop.
> 
> 
> 
> Another comment about the append with all those calls to float().  When 
> 
> you see a line like that, you want to seriously consider making it a 
> 
> loop, or a comprehension, or something.
> 
> 
> 
> Given that elem is a list of strings, you could convert it to a list of 
> 
> float, then convert that to a tuple (if you really wanted that).  At 
> 
> that point, you just append it.  All this could be done in one line if 
> 
> you like, something like (untested):
> 
> 
> 
>       object_data[i].append(tuple(map(float, row.split(","))))
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> DaveA

Yes, indeed there are a few lines missing. Just the read file and the i=0 pretty much. The help you gave me solved my problem, thank you very much!

I know the calls to float() are sub par and quite stupidly made but I'm not that good at python scripting (yet) so I try to keep my functions rather easy to read so I understand what is happening. A bit later I will take the time to make my code more sophisticated, but as of now I just want it to work, hehe.

Again, thank you so much!



More information about the Python-list mailing list