List comprehension vs. lambda + filter

前端 未结 14 2147
挽巷
挽巷 2020-11-22 02:01

I happened to find myself having a basic filtering need: I have a list and I have to filter it by an attribute of the items.

My code looked like this:



        
相关标签:
14条回答
  • 2020-11-22 02:59

    In addition to the accepted answer, there is a corner case when you should use filter instead of a list comprehension. If the list is unhashable you cannot directly process it with a list comprehension. A real world example is if you use pyodbc to read results from a database. The fetchAll() results from cursor is an unhashable list. In this situation, to directly manipulating on the returned results, filter should be used:

    cursor.execute("SELECT * FROM TABLE1;")
    data_from_db = cursor.fetchall()
    processed_data = filter(lambda s: 'abc' in s.field1 or s.StartTime >= start_date_time, data_from_db) 
    

    If you use list comprehension here you will get the error:

    TypeError: unhashable type: 'list'

    0 讨论(0)
  • 2020-11-22 03:00

    Filter is just that. It filters out the elements of a list. You can see the definition mentions the same(in the official docs link I mentioned before). Whereas, list comprehension is something that produces a new list after acting upon something on the previous list.(Both filter and list comprehension creates new list and not perform operation in place of the older list. A new list here is something like a list with, say, an entirely new data type. Like converting integers to string ,etc)

    In your example, it is better to use filter than list comprehension, as per the definition. However, if you want, say other_attribute from the list elements, in your example is to be retrieved as a new list, then you can use list comprehension.

    return [item.other_attribute for item in my_list if item.attribute==value]
    

    This is how I actually remember about filter and list comprehension. Remove a few things within a list and keep the other elements intact, use filter. Use some logic on your own at the elements and create a watered down list suitable for some purpose, use list comprehension.

    0 讨论(0)
  • 2020-11-22 03:00

    Here's a short piece I use when I need to filter on something after the list comprehension. Just a combination of filter, lambda, and lists (otherwise known as the loyalty of a cat and the cleanliness of a dog).

    In this case I'm reading a file, stripping out blank lines, commented out lines, and anything after a comment on a line:

    # Throw out blank lines and comments
    with open('file.txt', 'r') as lines:        
        # From the inside out:
        #    [s.partition('#')[0].strip() for s in lines]... Throws out comments
        #   filter(lambda x: x!= '', [s.part... Filters out blank lines
        #  y for y in filter... Converts filter object to list
        file_contents = [y for y in filter(lambda x: x != '', [s.partition('#')[0].strip() for s in lines])]
    
    0 讨论(0)
  • 2020-11-22 03:01

    It took me some time to get familiarized with the higher order functions filter and map. So i got used to them and i actually liked filter as it was explicit that it filters by keeping whatever is truthy and I've felt cool that I knew some functional programming terms.

    Then I read this passage (Fluent Python Book):

    The map and filter functions are still builtins in Python 3, but since the introduction of list comprehensions and generator ex‐ pressions, they are not as important. A listcomp or a genexp does the job of map and filter combined, but is more readable.

    And now I think, why bother with the concept of filter / map if you can achieve it with already widely spread idioms like list comprehensions. Furthermore maps and filters are kind of functions. In this case I prefer using Anonymous functions lambdas.

    Finally, just for the sake of having it tested, I've timed both methods (map and listComp) and I didn't see any relevant speed difference that would justify making arguments about it.

    from timeit import Timer
    
    timeMap = Timer(lambda: list(map(lambda x: x*x, range(10**7))))
    print(timeMap.timeit(number=100))
    
    timeListComp = Timer(lambda:[(lambda x: x*x) for x in range(10**7)])
    print(timeListComp.timeit(number=100))
    
    #Map:                 166.95695265199174
    #List Comprehension   177.97208347299602
    
    0 讨论(0)
  • 2020-11-22 03:01

    My take

    def filter_list(list, key, value, limit=None):
        return [i for i in list if i[key] == value][:limit]
    
    0 讨论(0)
  • 2020-11-22 03:04

    This is a somewhat religious issue in Python. Even though Guido considered removing map, filter and reduce from Python 3, there was enough of a backlash that in the end only reduce was moved from built-ins to functools.reduce.

    Personally I find list comprehensions easier to read. It is more explicit what is happening from the expression [i for i in list if i.attribute == value] as all the behaviour is on the surface not inside the filter function.

    I would not worry too much about the performance difference between the two approaches as it is marginal. I would really only optimise this if it proved to be the bottleneck in your application which is unlikely.

    Also since the BDFL wanted filter gone from the language then surely that automatically makes list comprehensions more Pythonic ;-)

    0 讨论(0)
提交回复
热议问题