map-function

Python: Call function that returns two strings inside map function

Python: Call function that returns two strings inside map function Question: Hello I am trying to create a function that is called inside a map function, splits the string that have been passed as input and returns two processed strings. To be more understood here is my code (it doesn’t seem to return anything). def …

Total answers: 2

Where did the "None"s come from? | Python map() function

Where did the "None"s come from? | Python map() function Question: I wrote a little code that uses the map() function: def function(a): if a % 2 == 0: return a else: pass x = map(function, (1,3,2,5,6)) print(set(x)) It’s supposed to return every even number that is provided, If it’s an odd number, I don’t …

Total answers: 1

Exclude null values in map function of Python3

Exclude null values in map function of Python3 Question: I am using map to process a list in Python3.6: def calc(num): if num > 5: return None return num * 2 r = map(lambda num: clac(num), range(1, 10)) print(list(r)) # => [2, 4, 6, 8, 10, None, None, None, None] The result I expect is: …

Total answers: 2

Python 'map' function inserting NaN, possible to return original values instead?

Python 'map' function inserting NaN, possible to return original values instead? Question: I am passing a dictionary to the map function to recode values in the column of a Pandas dataframe. However, I noticed that if there is a value in the original series that is not explicitly in the dictionary, it gets recoded to …

Total answers: 2

Can I use index information inside the map function?

Can I use index information inside the map function? Question: Let’s assume there is a list a = [1, 3, 5, 6, 8]. I want to apply some transformation on that list and I want to avoid doing it sequentially, so something like map(someTransformationFunction, a) would normally do the trick, but what if the transformation …

Total answers: 6

Read in a CSV file in lower case with Python

Read in a CSV file in lower case with Python Question: I’m reading a CSV file into a namedtuple as so: import csv from collections import namedtuple #So we can handle bad CSV files gracefully def unfussy_reader(reader): while True: try: yield next(reader.lower()) # This is a bad row that has an error in it (csv.Error) …

Total answers: 3

Passing multiple parameters to pool.map() function in Python

Passing multiple parameters to pool.map() function in Python Question: I need some way to use a function within pool.map() that accepts more than one parameter. As per my understanding, the target function of pool.map() can only have one iterable as a parameter but is there a way that I can pass other parameters in as …

Total answers: 3

Are list-comprehensions and functional functions faster than "for loops"?

Are list-comprehensions and functional functions faster than "for loops"? Question: In terms of performance in Python, is a list-comprehension, or functions like map(), filter() and reduce() faster than a for loop? Why, technically, they run in a C speed, while the for loop runs in the python virtual machine speed?. Suppose that in a game …

Total answers: 8

Python: Difference between filter(function, sequence) and map(function, sequence)

Python: Difference between filter(function, sequence) and map(function, sequence) Question: I’m reading through the Python documentation to really get in depth with the Python language and came across the filter and map functions. I have used filter before, but never map, although I have seen both in various Python questions here on SO. After reading about …

Total answers: 5

Python `map` and arguments unpacking

Python `map` and arguments unpacking Question: I know, that map(function, arguments) is equivalent to for argument in arguments: function(argument) Is it possible to use map function to do the following? for arg, kwargs in arguments: function(arg, **kwargs) Asked By: RadosÅ‚aw Miernik || Source Answers: You can with a lambda: map(lambda a: function(a[0], **a[1]), arguments) or …

Total answers: 4