Python中子列表的时间复杂度 [英] Time complexity for a sublist in Python

查看:87
本文介绍了Python中子列表的时间复杂度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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)

这篇关于Python中子列表的时间复杂度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆