>>> lst = [1,2,4,5]
>>> map(lambda x: 'lower' if x < 3 else 'higher', lst)
['lower', 'lower', 'higher', 'higher']
Aside: It's usually preferred to use a list comprehension for this
>>> ['lower' if x < 3 else 'higher' for x in lst]
['lower', 'lower', 'higher', 'higher']
Answer from John La Rooy on Stack Overflow>>> lst = [1,2,4,5]
>>> map(lambda x: 'lower' if x < 3 else 'higher', lst)
['lower', 'lower', 'higher', 'higher']
Aside: It's usually preferred to use a list comprehension for this
>>> ['lower' if x < 3 else 'higher' for x in lst]
['lower', 'lower', 'higher', 'higher']
Ternary operator:
map(lambda x: 'lower' if x<3 else 'higher', lst)
You'll have to wrap the map around a filter around the list:
example_map = map(lambda x: x*2, filter(lambda x: x*2/6. != 1, range(5)))
Alternatively, you could filter your map rather than maping your filter.
example_map = filter(lambda x: x/6. != 1, map(lambda x: x*2, range(5)))
Just remember that you're now filtering the RESULT rather than the original (i.e. lambda x: x/6. != 1 instead of lambda x: x*2/6. != 1 since x is already doubled from the map)
Heck if you really want, you could kind of throw it all together with a conditional expression
example_map = map(lambda x: x*2 if x*2/6. != 1 else None, range(5))
But it'll leave you with [0, 2, 4, None, 8]. filter(None, example_map) will drop the Nones and leave you [0, 2, 4, 8] as expected.
Try
lambda x: 1 if x == "C" else 0
possible duplicate of Is there a way to perform "if" in python's lambda
Example :
map(lambda x: True if x % 2 == 0 else False, range(1, 11))
result will be - [False, True, False, True, False, True, False, True, False, True]
It will be simpler to just do this:
df["Cherbourg"] = (df["Embarked"] == "C").astype('int)
Yes. if-else in Scala is a conditional expression, meaning it returns a value. You can use it as follows:
val result = list.map(x => if (x % 2 == 0) x * 2 else x / 2)
Which yields:
scala> val list = List(1,2,3,4,5,6)
list: List[Int] = List(1, 2, 3, 4, 5, 6)
scala> list.map(x => if (x % 2 == 0) x * 2 else x / 2)
res0: List[Int] = List(0, 4, 1, 8, 2, 12)
You could also write this as a PartialFunction which in some cases is easier to read, especially if you have several conditions:
val result = list.map{
case x if x % 2 == 0 => x * 2
case x => x / 2
}
map always produces one output item for each input item, it can not remove elements. Furthermore, map should not be used to mutate objects, that's not its job, and because it's lazy the results can be unexpected.
filter is designed to create an output with less elements than the input, although it's mostly useful if you already have a ready-made predicate (filtering) function.
Since you do not, you can and should use comprehensions which provide a relatively terse way to perform iteration, filtering, mapping and collection in a single construct:
wordlist = ['hello','world','Tom']
checklist = ['hello','world']
print('before')
print(wordlist)
wordlist = [word for word in wordlist if word not in checklist]
print('after')
print(wordlist)
ps: if you want to modify things in-place, use a regular loop
You can try this instead, which doesn't use lambda but accomplishes your goal. Let us know if you absolutely must use lambda. The issue with your lambda expression is that your modifying the list that you are providing to map in the lambda function.
wordlist_2 [word for word in wordlist if word not in checklist and word]
The last and word is to not add None to your list.
Your conditions are sequential in nature; you want to test one after the other, not map a small number of keys to a value here. Changing the order of the conditions could alter the outcome; a value of 5 results in "greater than 0.5" in your sample, not "it is equal to 5".
Use a list of tuples:
myconditions = [
(lambda i: i > 0.5, "greater than 0.5"),
(lambda i: i == 5, "it is equal to 5"),
(lambda i: i > 5 and i < 6, "somewhere between 5 and 6"),
]
after which you can access each one in turn until one matches:
for test, message in myconditions:
if test(i):
return message
Re-ordering the tests will change the outcome.
A dictionary works for your first example because there is a simple equality test against multiple static values that is optimised by a dictionary, but there are no such simple equalities available here.
You can't use a dictionary to map arbitrary conditionals since more than one of them could be true at the same time. Instead you need to evaluate each one sequentially and execute the associated code the first time a true one is encountered. Here's an outline of one way to formally implement something like that which even allows the equivalent of a default: case.
from collections import namedtuple
Case = namedtuple('Case', ['condition', 'code'])
cases = (Case('i > 0.5',
"""print 'greater than 0.5'"""),
Case('i == 5',
"""print 'it is equal to 5'"""),
Case('i > 5 and i < 6',
"""print 'somewhere between 5 and 6'"""))
def switch(cases, **namespace):
for case in cases:
if eval(case.condition, namespace):
exec(case.code, namespace)
break
else:
print 'default case'
switch(cases, i=5)
Output:
greater than 0.5