Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Worth noting that Counter itself uses what the article calls get Method, but with a common performance optimization (caching a bound method).

    def _count_elements(mapping, iterable):
        mapping_get = mapping.get
        for elem in iterable:
            mapping[elem] = mapping_get(elem, 0) + 1


Also worth noting that those lines are immediately followed by:

    try:                                    # Load C helper function if available        
        from _collections import _count_elements
    except ImportError:
        pass




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: