Python: what is the fastest way to map or compress calls and ignore errors?

点点圈 提交于 2019-12-23 04:43:18

问题


I frequently encounter a problem where I need to apply a function to a large iterator of data, but that function sometimes raises a known error that I want to ignore. Unfortunately, neither list compressions nor the map function has a good way to handle errors.

What is the best way to skip/deal with errors quickly in python?

For example, say I have a list of data and a function, the function raises a ValueError whenever the data is a str. I want it to skip these values. One way to do this would be:

result = []
for n in data:
    try: result.append(function(n))
    except ValueError: pass

You could also do the same thing without the error checking like:

result = [function(n) for n in data]

or

result = list(map(function, data))

I want an c-compiled approach to accomplishing the above. Something in the spirit of

result = list(map(function, data, skip_errors=True))

The feature of default=value would also be useful, so that raised errors create a default value.

I'm thinking this might be something I need to write a Cython extension for.

Note: one solution would be for me to write the catch function I wrote in this answer in c or cython. Then I could use it in list compressions and get the performance boost I want.


回答1:


Why not just wrap your function in an error handler?

def spam(n):
    try:
        return function(n)
    except ValueError:
        pass
result = [spam(n) for n in data]

you can then add anything you want to the error handling (note that in this version it returns None, so you probably want to either filter the resulting list or return a default value). The same goes for using map.



来源:https://stackoverflow.com/questions/28816432/python-what-is-the-fastest-way-to-map-or-compress-calls-and-ignore-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!