473,473 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1363
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: max | last post by:
I am trying to use a sdk to access quickbooks, the sdk provided a COM interface and I think it installed the com server files/dlls. A VB code example is provided, but I can't figure out how to map...
9
by: Jerry Sievers | last post by:
Fellow Pythonists; I am totally puzzled on the use of slicing on mapping types and especially unsure on use of the Ellipsis... and slicing syntax that has two or more groups seperated by comma....
6
by: naruto | last post by:
Hi all, I have the following being defined in a A.cxx file. // define in source file. Not exported to the outside world (this cannot be // moved to the header file ) #define CHANNEL_0 0...
1
by: none | last post by:
Hi, I'm trying to establish table mappings, and I've hit a snag. At the point to where I try to fill the schema (DB_adapter.FillSchema), I get an exception, and the message is as follows:...
0
by: Arthur Dent | last post by:
I have a PageBase class I use which has a bunch of properties to get various "path" components for the page... such as: protocol: http server: www.myserver.com full: ...
3
by: Lee Sander | last post by:
hi, I have the following problem which is turning out to be non-trivial. I realize that this is not exactly a python problem but more of an algorithm problem -- but I post it here because I want...
2
by: devnew | last post by:
hi i am looking for some info about mapping btw values in an array and corresponding columns of a matrix i have an numpy array= and a numpy matrix object= matrix((, , , ))
19
by: dave | last post by:
Hi All, I wrote a program that takes a string sequence and finds all the words inside a text file (one word per line) and prints them: def anagfind(letters): #find anagrams of these letters...
15
by: Paddy | last post by:
Iam wondering why the peculiar behavior of map when the function in given as None: Help on built-in function map in module __builtin__: map(...) map(function, sequence) -list Return a list...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.