最佳答案
我在研究列表理解法时,在另一个网站上偶然发现了这个小片段:
return ''.join([`num` for num in xrange(loop_count)])
我花了几分钟试图复制这个函数(通过键入) ,然后才意识到 `num`
位正在破坏它。
在这些字符中包含一个语句有什么作用?在我看来,它等价于 str (num)。但是当我计时的时候:
return ''.join([str(num) for num in xrange(10000000)])
它需要4.09秒,然而:
return ''.join([`num` for num in xrange(10000000)])
需要2.43秒。
两者得到的结果相同,但是其中一个要慢得多。这是怎么回事?
奇怪的是... repr()
的结果比 `num`
稍慢。2.99秒对2.43秒。我正在使用 Python 2.6(还没有尝试过3.0)。