Time complexity for a sublist in Python

我的未来我决定 提交于 2019-12-13 13:12:16

问题


In Python, what is the time complexity when we create a sublist from an existing list?

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]

What is the running time for creating list1?

Is it O(10^6) i.e.O(N), or O(1)?

回答1:


Getting a list slice in python is O(M - N) / O(10^6 - 101)

Here you can check python list operations time complexity

By underneath, python lists are represented like arrays. So, you can iterate starting on some index(N) and stopping in another one(M)



来源:https://stackoverflow.com/questions/39338520/time-complexity-for-a-sublist-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!