A loop is good enough for this task, but if for some reason you must use lambda and map, you can do it like this:
list(map(lambda x: print(f"I only have {x} friends, but they are awesome."), range(2, 5)))
The expression needs to be wrapped in list() as the map function is eagerly executed, which means it doesn't calculate the output until it is needed.
How to write a Python for loop as a lambda function with apply or map instead? - Stack Overflow
Python: Using Map/Lambda instead of for loop - Stack Overflow
Does the built-in map() in python actually doesn't use loops? How does the map iterate?
python - Lambda Range For Loop - Stack Overflow
A loop is good enough for this task, but if for some reason you must use lambda and map, you can do it like this:
list(map(lambda x: print(f"I only have {x} friends, but they are awesome."), range(2, 5)))
The expression needs to be wrapped in list() as the map function is eagerly executed, which means it doesn't calculate the output until it is needed.
you don't just use lambda or map to execute the above code, But if you want to achieve this with lambda or map only, you need to convert the map object into a list.
list(map(lambda x:print(f'I only have {x} friends, but they are awesome.'), range(2,5)))
I was going through this map() function in python and interestingly in all tutorials, it is mentioned. We can use map() instead of loops to make a transformation on an iterable. Does the map function inside uses for loop or while loop? how does it iterate then? also, what is the Big O notion for map?
Probably easier to use list comprehensions:
print (lambda w, x=10, y=range(10):\
[(0 if (i >= w*w and i < x and i%w==0) else n) for i, n in enumerate(y)])(2)
Moved to two lines for readability, but you can delete the \ and the line break and it will run fine.
One caveat is that this doesn't alter the original list, but returns a new one.
If you needed it to update the original list AND return it, you could use short-circuiting:
print (lambda w, x=10, y=range(10):\
([y.__setitem__(i, 0) for i in range(w*w, x, w)] and y))(2)
Correction:
The code above works only if range(w*w, x, w) is non-empty, i.e. w*w > x, which is a weak condition.
The following corrects for this issue:
print (lambda w, x=10, y=range(10):\
(([y.__setitem__(i, 0) for i in range(w*w, x, w)] or 1) and y))(2)
This uses the fact that (a or 1) and b always evaluates to b after the value of a gets evaluated.
Here's a pure lambda implementation, which takes w, x, and y as arguments to a top-level lambda:
>>> (lambda w,x,y: (lambda s: map(lambda v: 0 if v in s else v, y))(set(range(w*w,x,w))))(2,10,range(10))
[0, 1, 2, 3, 0, 5, 0, 7, 0, 9]
>>>
Note that this avoids the use of __setitem__.
- Python style guide recommends using list comprehensions instead of map/reduce
- String formatting using percent operator is obsolete, consider using format() method
the code you need is this simple one-liner
output = [" this string contains {} and {}".format(x, y) for (x, y) in matrix]
You have a couple of issues, these structures aren't nested deeply enough to warrant the nested loops.
You need 1 map for each level of list you wish to process, so if you want to process a list, you need a map, if you want to process a list of lists, you need 2 and so on.
In this case you most likely only want to process the top level (effectively this is because you want each list in the top level to become a sentence).
def sentence( x, y):
return " this string contains %s and %s" % (x,y)
matrix = [['a','b'],['c','d']]
output = map(lambda a: sentence(a[0],a[1]), matrix)
# Print the top level
for i in output:
print(i)