NEWB: General purpose list iteration?

Donald Newcomb DRNewcomb at attglobal.NOT.net
Thu Aug 11 22:22:19 EDT 2005


I'm a real Python NEWB and am intrigued by some of Python's features, so I'm
starting to write code to do some things to see how it works. So far I
really like the lists and dictionaries since I learned to love content
addressability in MATLAB.  I was wondering it there's a simple routine (I
think I can write a recurisve routine to do this.) to scan all the elements
of a list, descending to lowest level and change something. What I'd like to
do today is to convert everything from string to float. So, if I had a list
of lists that looked like:
[['1.1', '1.2', '1.3'], [['2.1', '2.2'], [['3.1', '3.2'], ['4.1', '4.2']]]]
and I'd like it to be:
[[1.1, 1.2, 1.3], [[2.1, 2.2], [[3.1, 3,2], [4.1, 4.2]]]]
is there just a library routine I can call to do this? Right now, I'm using
'for' loops nested to the maximum depth anticipated.

for i in range(len(list)):
    for j in range(len(list[i])):
        for k in range(len(list[i][j])):
            etc
                 list[i][j][...] = float(list[i][j][....])

Which works but is not pretty. I was just looking for a way to say:
listb = float(lista)
However, the way I've started jamming everything into lists and
dictionaries, I'm sure I'll be needing to do other in-place conversions
similar to this in the future.

-- 
Donald Newcomb
DRNewcomb (at) attglobal (dot) net





More information about the Python-list mailing list