473,788 Members | 2,800 Online
Bytes | Software Development & Data Engineering Community
+ 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 1378
> 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*******@hotma il.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*******@hotma il.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.i t>:
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.i t>:
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***********@a cm.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.i t>:
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.i t>:
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
1905
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 "Dim sessionManager As New QBFC2_1Lib.QBSessionManager" into a corresponding Python statement, when I try obj = win32com.Client.Dispatch("QBFC2_1Lib.QBSessionManager") I get traceback Shown below. I have been able to run makepy and it...
9
2477
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. I am referring to (from the manual); Slice objects Slice objects are used to represent slices when extended
6
6545
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 #define CHANNEL_1 1 #define CHANNEL_2 2
1
1716
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: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Invalid column name 'Unique_ref'. Invalid column name 'ID_string'. Invalid column name 'Sequence'.
0
1274
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: http://www.myserver.com/index.aspx file: index.aspx path: http://www.myserver.com/ and so on. They all work beautifully. Until I went and added a url mapping. I added a URL mapping of
3
1304
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 to implement this in python. I want to write a code that given an interval (integer tuple: start,stop) will find which other interval it matches to. Here is an example,
2
3423
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
2804
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 fin = open('text.txt') #one word per line file wordbox = #this is where the words will go for line in fin: word = line.strip()
15
1383
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 of the results of applying the function to the items of
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10364
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.