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 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 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) ]) 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]
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
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
"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
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
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. :-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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....
|
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...
|
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:...
|
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: ...
| |
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...
|
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((,
,
,
))
|
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...
|
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...
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |