Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?

前端 未结 11 1398
梦毁少年i
梦毁少年i 2020-11-22 03:46

It is my understanding that the range() function, which is actually an object type in Python 3, generates its contents on the fly, similar to a generator.

11条回答
  •  攒了一身酷
    2020-11-22 04:07

    TLDR; range is an arithmetic series so it can very easily calculate whether the object is there.It could even get the index of it if it were list like really quickly.

提交回复
热议问题