Most elegant way to do something N times

Marco Sulla mail.python.org at marco.sulla.e4ward.com
Tue Dec 24 21:09:09 EST 2019


Anyway, from itertools recipes:

def repeatfunc(func, times=None, *args):
    """Repeat calls to func with specified arguments.

    Example:  repeatfunc(random.random)
    """
    if times is None:
        return starmap(func, repeat(args))
    return starmap(func, repeat(args, times))

On Tue, 24 Dec 2019 at 21:41, Marco Sulla <elbarbun at gmail.com> wrote:
>
> On Tue, 24 Dec 2019 at 01:07, Chris Angelico <rosuav at gmail.com> wrote:
> > On Tue, Dec 24, 2019 at 10:45 AM Marco Sulla <...> wrote:
> > > ??? Excuse me, but why you needed to call the same function SIX times? This
> > > seems to me not elegant in primis.
> > >
> > > Can you give us a practical example?
> >
> > File parsing. You read a section header and want to ignore that
> > section, so you ignore the next 15 lines.
>
> mmap and find?
>
>
> On Tue, 24 Dec 2019 at 01:35, DL Neil via Python-list
> <python-list at python.org> wrote:
> > Taking the top/bottom six from a sorted list of occurrences.
>
> Slicing?


More information about the Python-list mailing list