2D List comprehension & replace
lbates at swamisoft.com
Fri Mar 12 01:28:21 CET 2004
I should not have given up so soon.
[[y or None for y in x] for x in l]
seems to do the trick.
"Larry Bates" <lbates at swamisoft.com> wrote in message
news:YuSdndB1QM85nczd4p2dnA at comcast.com...
> I've gone brain-dead trying to get this to work
> using list comprehensions. I have a list of lists
> like the following:
> l=[[92.6, 0, 93.5],
> [94.7, 95.6, 95.7],
> [91.2, 92.1, 93.0]]
> what I want is a new list
> with the zero (0) replaced with None.
> I thought I would use the fact that the command:
> 0 or None
> seems to return None.
> This procedural code does the trick, but I just
> know it can be done with a single list comprehension.
> for a in l:
> for v in a:
> if a != 0: til.append(v)
> else: til.append(None)
> Thanks in advance.
> Larry Bates
More information about the Python-list