2021年4月29日星期四

Python Map apply same function name but different functions

So I want to apply a function to values using map where each function is dependent upon the value.

For example, in my problem, I have a list of objects where each have their own personalized function called predict and I want to apply each of these functions to the same input. How would I do this using map?

More specifically, I have different model objects where each model has their own specialized predict function that is not similar to the rest. For this list of different model objects, I want to apply their predict function to the same data set.

Simple Example:

class Model1:        def predict(self, value):          return value ** 1    class Model2:        def predict(self, value):          return value ** 2 + value / 3    class Model3:        def predict(self, value):          return value ** 3 * value    m1 = Model1  m2 = Model2  m3 = Model3  models = [m1, m2, m3]  data = [1,3,2,34]  result = map(models.predict, data) # how to do this step??  

NOTE: I want to do this with the map function, I know how to do this without.

https://stackoverflow.com/questions/67327020/python-map-apply-same-function-name-but-different-functions April 30, 2021 at 09:19AM

没有评论:

发表评论