469,923 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

2D List comprehension & replace

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.

tol=[]
for a in l:
til=[]
for v in a:
if a != 0: til.append(v)
else: til.append(None)

tol.append(til)

l=tol

Thanks in advance.

Larry Bates
Jul 18 '05 #1
5 3182
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
"Larry Bates" <lb****@swamisoft.com> wrote in message
news:Yu********************@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.

tol=[]
for a in l:
til=[]
for v in a:
if a != 0: til.append(v)
else: til.append(None)

tol.append(til)

l=tol

Thanks in advance.

Larry Bates

Jul 18 '05 #2
"Larry Bates" <lb****@swamisoft.com> wrote in message
news:ZO********************@comcast.com...
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

Well if that works, then this must too:

[y or None for x in l for y in x]

-- Paul


"Larry Bates" <lb****@swamisoft.com> wrote in message
news:Yu********************@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.

tol=[]
for a in l:
til=[]
for v in a:
if a != 0: til.append(v)
else: til.append(None)

tol.append(til)

l=tol

Thanks in advance.

Larry Bates


Jul 18 '05 #3
Paul McGuire wrote:
"Larry Bates" <lb****@swamisoft.com> wrote in message
news:ZO********************@comcast.com...
[[y or None for y in x] for x in l]


Well if that works, then this must too:

[y or None for x in l for y in x]


No, that will give you a single-level list,
not a list of lists.

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

Jul 18 '05 #4
"Greg Ewing (using news.cis.dfn.de)" <wm*******@sneakemail.com> wrote in
message news:c2*************@ID-169208.news.uni-berlin.de...
Paul McGuire wrote:
"Larry Bates" <lb****@swamisoft.com> wrote in message
news:ZO********************@comcast.com...
[[y or None for y in x] for x in l]


Well if that works, then this must too:

[y or None for x in l for y in x]


No, that will give you a single-level list,
not a list of lists.

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

Shoot! Well, thanks for straightening me out! I'll fix this in my Matlab
wrapper class, too.

-- Paul
Jul 18 '05 #5
On Thu, 11 Mar 2004 18:13:28 -0600, "Larry Bates"
<lb****@swamisoft.com> wrote:
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.

tol=[]
for a in l:
til=[]
for v in a:
if a != 0: til.append(v)
else: til.append(None)

tol.append(til)

l=tol

Thanks in advance.

Larry Bates


def NonifyNestedList(rows):
return [[item or None for item in cols] for cols in rows]

--dang
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Guy Robinson | last post: by
35 posts views Thread by Moosebumps | last post: by
15 posts views Thread by Darren Dale | last post: by
3 posts views Thread by Matt Gerrans | last post: by
6 posts views Thread by jena | last post: by
4 posts views Thread by bullockbefriending bard | last post: by
5 posts views Thread by Pat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.