在Python中,当我们从现有列表创建子列表时,时间复杂度是多少?
In Python, what is the time complexity when we create a sublist from an existing list?
例如,这里的data是我们现有列表的名称,而list1是通过对数据进行切片而创建的子列表.
For example, here data is the name of our existing list and list1 is our sublist created by slicing data.
data = [1,2,3,4,5,6..100,...1000....,10^6] list1 = data[101:10^6]创建list1的运行时间是多少?
What is the running time for creating list1?
Is it O(10^6) i.e.O(N), or O(1)?推荐答案
在python中获取列表切片为O(M - N)/O(10^6 - 101)
Getting a list slice in python is O(M - N) / O(10^6 - 101)
此处,您可以检查python列表操作的时间复杂度
Here you can check python list operations time complexity
在下面,python列表表示为数组.因此,您可以从一个索引(N)开始迭代,然后再从另一个索引(M)终止
By underneath, python lists are represented like arrays. So, you can iterate starting on some index(N) and stopping in another one(M)