python中map(int、input())_使用map(int,raw_input().split())

如果您使用内置功能的map,那么它可能比LC稍快:>>> strs = " ".join(str(x) for x in xrange(10**5))

>>> %timeit [int(x) for x in strs.split()]

1 loops, best of 3: 111 ms per loop

>>> %timeit map(int, strs.split())

1 loops, best of 3: 105 ms per loop

使用用户定义的函数:>>> def func(x):

... return int(x)

>>> %timeit map(func, strs.split())

1 loops, best of 3: 129 ms per loop

>>> %timeit [func(x) for x in strs.split()]

1 loops, best of 3: 128 ms per loop

Python 3.3.1比较:>>> strs = " ".join([str(x) for x in range(10**5)])

>>> %timeit list(map(int, strs.split()))

10 loops, best of 3: 59 ms per loop

>>> %timeit [int(x) for x in strs.split()]

10 loops, best of 3: 79.2 ms per loop

>>> def func(x):

return int(x)

...

>>> %timeit list(map(func, strs.split()))

10 loops, best of 3: 94.6 ms per loop

>>> %timeit [func(x) for x in strs.split()]

1 loops, best of 3: 92 ms per loopThe only restriction is that the "loop body" of map must be a function

call. Besides the syntactic benefit of list comprehensions, they are

often as fast or faster than equivalent use of map.


版权声明:本文为weixin_39942572原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。