469,313 Members | 2,639 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

mapping None values to ''

hi
i wish to map None or "None" values to "".
eg
a = None
b = None
c = "None"

map( <something> , [i for i in [a,b,c] if i in ("None",None) ])

I can't seem to find a way to put all values to "". Can anyone help?
thanks

Jun 18 '06 #1
8 1221
> i wish to map None or "None" values to "".
eg
a = None
b = None
c = "None"

map( <something> , [i for i in [a,b,c] if i in ("None",None) ])

I can't seem to find a way to put all values to "". Can anyone help?
thanks


I'd consider this a VeryBadIdea(tm). However, given Python's
introspective superpowers, it *can* be done (even if it most
likely *shouldn't* be done). That caveat out of the way, here goes:
for scope in [locals, globals]:

.... s = scope()
.... for vbl, value in s.items():
.... if value == None or (type(value) == type("") and
value == 'None'):
.... s[vbl] = ''
seems to do the trick. You may, likely, just want to operate on
locals, not every last global variable in your project, in which
case you can just use

s = locals()
for vbl, value in s.items():
...

There may be some better way to determing whether an item is a
string than my off-the-cufff

type(value) == type("")

but it worked to prevent trying to compare non-strings to the
string 'None' later in the sub-clause of the if statement.

Note that if you wrap this in a function, you'll may have to pass
in locals() as a parameter because otherwise, inside your
function, you'll have a different set of available locals that
you did when you called the function. :)

Beware your foot when shooting this gun...it likes to aim at feet.

-tkc


Jun 18 '06 #2
mi*******@hotmail.com ha scritto:
hi
i wish to map None or "None" values to "".
eg
a = None
b = None
c = "None"

map( <something> , [i for i in [a,b,c] if i in ("None",None) ])

I can't seem to find a way to put all values to "". Can anyone help?
thanks


You already filtered [a,b,c] in the comprehension list, so you just have
to map all its values to "":

map(lambda x:"" , [i for i in [a,b,c] if i in ("None",None) ])
Jun 18 '06 #3

mi*******@hotmail.com wrote:
hi
i wish to map None or "None" values to "".
eg
a = None
b = None
c = "None"

map( <something> , [i for i in [a,b,c] if i in ("None",None) ])

I can't seem to find a way to put all values to "". Can anyone help?
thanks


a = [None, 'None', None]

def filtre(x):
if x is None or x == 'None':
return ''
else:
return x

assert map(filtre, a) == ['', '', '']

a = [1, 2, None, 'None', 3, 4]

assert map(filtre, a) == [1, 2, '', '', 3, 4]

Jun 18 '06 #4
imho <ce***@comeno.it>:
map(lambda x:"" , [i for i in [a,b,c] if i in ("None",None) ])


You don't need map when using list comprehensions:

["" for i in [a, b, c] if i in ("None", None)]

--
Roberto Bonvallet
Jun 18 '06 #5
Roberto Bonvallet wrote:
imho <ce***@comeno.it>:
map(lambda x:"" , [i for i in [a,b,c] if i in ("None",None) ])

You don't need map when using list comprehensions:
["" for i in [a, b, c] if i in ("None", None)]

More like:

[(i, "")[i in ("None", None)] for i in [a,b,c]]

--
--Scott David Daniels
sc***********@acm.org
Jun 18 '06 #6
"Roberto Bonvallet" <rb******@gmail.com> wrote:
You don't need map when using list comprehensions:

["" for i in [a, b, c] if i in ("None", None)]


That loses list elements that aren't in the tests:
a=7
b="None"
c=None
["" for i in [a,b,c] if i in ("None",None)] ['', '']


max

Jun 18 '06 #7
Scott David Daniels wrote:
Roberto Bonvallet wrote:
imho <ce***@comeno.it>:
map(lambda x:"" , [i for i in [a,b,c] if i in ("None",None) ])

You don't need map when using list comprehensions:
["" for i in [a, b, c] if i in ("None", None)]

More like:

[(i, "")[i in ("None", None)] for i in [a,b,c]]


Or in Python 2.5:

Python 2.5a2 (trunk:46491M, May 27 2006, 14:43:55) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
a = 0
b = None
c = None
a, b, c = ('' if x in (None, 'None') else x for x in (a, b, c))
a, b, c

(0, '', '')

But someone please shoot me if I ever use something like that in
production code. ;)

STeVe
Jun 19 '06 #8
Roberto Bonvallet ha scritto:
imho <ce***@comeno.it>:
map(lambda x:"" , [i for i in [a,b,c] if i in ("None",None) ])


You don't need map when using list comprehensions:

["" for i in [a, b, c] if i in ("None", None)]


I know that... I tried to match the idiom used by the o.p. :-)
Jun 19 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by max | last post: by
9 posts views Thread by Jerry Sievers | last post: by
6 posts views Thread by naruto | last post: by
1 post views Thread by none | last post: by
reply views Thread by Arthur Dent | last post: by
3 posts views Thread by Lee Sander | last post: by
19 posts views Thread by dave | last post: by
15 posts views Thread by Paddy | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.