python中各种结构的复杂度

list

The Average Case assumes parameters generated uniformly at random.

Internally, a list is represented as an array; the largest costs come from growing beyond the current allocation size (because everything must move), or from inserting or deleting somewhere near the beginning (because everything after that must move). If you need to add/remove at both ends, consider using a collections.deque instead.


Operation


Average Case


Amortized Worst Case


Copy


O(n)


O(n)


Append[1]


O(1)


O(1)


Insert


O(n)


O(n)


Get Item


O(1)


O(1)


Set Item


O(1)


O(1)


Delete Item


O(n)


O(n)


Iteration


O(n)


O(n)


Get Slice


O(k)


O(k)


Del Slice


O(n)


O(n)


Set Slice


O(k+n)


O(k+n)


Extend[1]


O(k)


O(k)


Sort


O(n log n)


O(n log n)


Multiply


O(nk)


O(nk)


x in s


O(n)


min(s), max(s)


O(n)


Get Length


O(1)


O(1)

collections.deque

A deque (double-ended queue) is represented internally as a doubly linked list. (Well, a list of arrays rather than objects, for greater efficiency.) Both ends are accessible, but even looking at the middle is slow, and adding to or removing from the middle is slower still.


Operation


Average Case


Amortized Worst Case


Copy


O(n)


O(n)


append


O(1)


O(1)


appendleft


O(1)


O(1)


pop


O(1)


O(1)


popleft


O(1)


O(1)


extend


O(k)


O(k)


extendleft


O(k)


O(k)


rotate


O(k)


O(k)


remove


O(n)


O(n)

set

See dict -- the implementation is intentionally very similar.


Operation


Average case


Worst Case


x in s


O(1)


O(n)


Union s|t


O(len(s)+len(t))


Intersection s&t


O(min(len(s), len(t))


O(len(s) * len(t))


Difference s-t


O(len(s))


s.difference_update(t)


O(len(t))


Symmetric Difference s^t


O(len(s))


O(len(s) * len(t))


s.symmetric_difference_update(t)


O(len(t))


O(len(t) * len(s))

  • As seen in the source code the complexities for set difference s-t or s.difference(t) (set_difference()) and in-place set difference s.difference_update(t) (set_difference_update_internal()) are different! The first one is O(len(s)) (for every element in s add it to the new set, if not in t). The second one is O(len(t)) (for every element in t remove it from s). So care must be taken as to which is preferred, depending on which one is the longest set and whether a new set is needed.
  • To perform set operations like s-t, both s and t need to be sets. However you can do the method equivalents even if t is any iterable, for example s.difference(l), where l is a list.

dict

The Average Case times listed for dict objects assume that the hash function for the objects is sufficiently robust to make collisions uncommon. The Average Case assumes the keys used in parameters are selected uniformly at random from the set of all keys.

Note that there is a fast-path for dicts that (in practice) only deal with str keys; this doesn‘t affect the algorithmic complexity, but it can significantly affect the constant factors: how quickly a typical program finishes.


Operation


Average Case


Amortized Worst Case


Copy[2]


O(n)


O(n)


Get Item


O(1)


O(n)


Set Item[1]


O(1)


O(n)


Delete Item


O(1)


O(n)


Iteration[2]


O(n)


O(n)

https://wiki.python.org/moin/TimeComplexity

来自为知笔记(Wiz)

时间: 2024-08-24 07:03:46

python中各种结构的复杂度的相关文章

Python中的结构化数据分析利器-Pandas简介

Pandas是python的一个数据分析包,最初由AQR Capital Management于2008年4月开发,并于2009年底开源出来,目前由专注于Python数据包开发的PyData开发team继续开发和维护,属于PyData项目的一部分.Pandas最初被作为金融数据分析工具而开发出来,因此,pandas为时间序列分析提供了很好的支持. Pandas的名称来自于面板数据(panel data)和python数据分析(data analysis).panel data是经济学中关于多维数

PythonStudy——Python 中Switch-Case 结构的实现

学习Python过程中,发现Python没有Switch-case,过去写C习惯用Switch/Case语句,官方文档说通过if-elif实现.所以不妨自己来实现Switch-Case功能. 方法一 通过字典实现 def foo(var): return { 'a': '1', 'b': '2', 'c': '3' }.get(var, 'error') # 'error'为默认返回值,可自设置 print(foo('a')) print(foo('b')) print(foo('c')) Ou

python 中分支结构(switch)

可通过字典调用:{1:case1,2:case2}.get(x,lambda *args,**key:)() # 编写一个计算器 # -*- coding=utf-8 -*- def jia(x,y): return x+y def jian(x,y): return x-y def cheng(x,y):  return x*y def chu(x,y): return (x/y) # def panduan(x,i,y): # if i=="+": #  jia(x,y) # el

使用C语言为python编写动态模块(2)--解析python中的对象如何在C语言中传递并返回

楔子 编写扩展模块,需要有python源码层面的知识,我们之前介绍了python中的对象.但是对于编写扩展模块来讲还远远不够,因为里面还需要有python中模块的知识,比如:如何创建一个模块.如何初始化python环境等等.因此我们还需要了解一些前奏的知识,如果你的python基础比较好的话,那么我相信你一定能看懂,当然我们一开始只是介绍一个大概,至于细节方面我们会在真正编写扩展模块的时候会说. 关于使用C为python编写扩展模块,我前面还有一篇博客,强烈建议先去看那篇博客,对你了解Pytho

Python中三种基本结构的语句

选择语句 if 条件判断 : # 条件可以加括号也可以不加括号 -- else: -- Python中没有switch语句这是可以使用if exp:.... elif exp:来代替 1 if 判断条件1: 2 执行语句1-- 3 elif 判断条件2: 4 执行语句2-- 5 elif 判断条件3: 6 执行语句3-- 7 else: 8 执行语句4-- Python 循环语句 while 循环 在给定的判断条件为 true 时执行循环体,否则退出循环体. for 循环 重复执行语句 嵌套循环

Python中对复杂数据结构排序(类似C中结构体数据结构)

Python中排序主要有两个函数:sorted和列表成员函数sort,两者除了调用方式有些区别外,最显著的区别是sorted会新建一个排序好的列表并返回,而sort是修改原列表并排好序.sorted的原型是: sorted(iterable, cmp=None, key=None, reverse=False) sort的原型是: list.sort(cmp=None, key=None, reverse=False) 其中cmp和key都是函数引用,即可以传入函数名.这两个函数都是对list里

Python的collections模块中namedtuple结构使用示例

namedtuple顾名思义,就是名字+元组的数据结构,下面就来看一下Python的collections模块中namedtuple结构使用示例 namedtuple 就是命名的 tuple,比较像 C 语言中 struct.一般情况下的 tuple 是 (item1, item2, item3,...),所有的 item 都只能按照 index 访问,没有明确的称呼,而 namedtuple 就是事先把这些 item 命名,以后可以方便访问. ? 1 2 3 4 5 6 7 8 9 10 11

Python中的循环结构

Python中的比较运算符: Python中的关系运算符: 循环是为了解决重复出现的问题而设计. 1. for 实现 1+..+100求和 1 sum=0 2 for i in range(1,101): #range(start,stop,step) 3 sum+=i 2. while 1 i=1 2 sum=0 3 while i<101: 4 sum+=i 5 i+=1 注意: 在Python没有++ 和 -- 运算符 循环语句与判断条件语句结合使用时,注意break跳出所在最小循环,而c

python中的行结构和缩进

程序中每条语句都以换行符结束 可以使用续行符(\)反斜杠将长语句分为几行  wather_is_hot  = 1;  watherer = 0;  if(wather_is_hot ==1) and    (watherer==0):    print("杯子里的水是热的");  有两种列外的情况下 一个语句不适用反斜线也可以跨行  1,单一语句可以跨行 如:含有 小括号 中括号 花括号时可以多行书写  例:  dict = {   'title':'行者',   'name' :'武