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 |
|
|
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) |
|
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 |
||
|
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) |
python中各种结构的复杂度的更多相关文章
- Python中的结构化数据分析利器-Pandas简介
Pandas是python的一个数据分析包,最初由AQR Capital Management于2008年4月开发,并于2009年底开源出来,目前由专注于Python数据包开发的PyData开发tea ...
- PythonStudy——Python 中Switch-Case 结构的实现
学习Python过程中,发现Python没有Switch-case,过去写C习惯用Switch/Case语句,官方文档说通过if-elif实现.所以不妨自己来实现Switch-Case功能. 方法一 ...
- python 中分支结构(switch)
可通过字典调用:{1:case1,2:case2}.get(x,lambda *args,**key:)() # 编写一个计算器 # -*- coding=utf-8 -*- def jia(x,y) ...
- Python中三种基本结构的语句
选择语句 if 条件判断 : # 条件可以加括号也可以不加括号 -- else: -- Python中没有switch语句这是可以使用if exp:.... elif exp:来代替 if 判断条件1 ...
- Python的collections模块中namedtuple结构使用示例
namedtuple顾名思义,就是名字+元组的数据结构,下面就来看一下Python的collections模块中namedtuple结构使用示例 namedtuple 就是命名的 tuple,比较 ...
- Python中的两种结构dict和set
Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 假设要根据同学的名字查找对应的成绩 如果 ...
- Python中的高级数据结构详解
这篇文章主要介绍了Python中的高级数据结构详解,本文讲解了Collection.Array.Heapq.Bisect.Weakref.Copy以及Pprint这些数据结构的用法,需要的朋友可以参考 ...
- python中的logger模块
logger 提供了应用程序可以直接使用的接口handler将(logger创建的)日志记录发送到合适的目的输出filter提供了细度设备来决定输出哪条日志记录formatter决定日志记录的最终输出 ...
- 3、顺序表、内存、类型、python中的list
1.内存.类型本质.连续存储 1.内存本质 2.C 语言实例-计算 int, float, double 和 char 字节大小 使用 sizeof 操作符计算int, float, double 和 ...
随机推荐
- Mysql 视图 游标 触发器 存储过程 事务
Mysql 视图 触发器 存储过程 游标 游标是从数据表中提取出来的数据,以临时表的形式存放在内存中,在游标中有一个数据指针,在初始状态下指向的是首记录,利用fetch语句可以移动该指针,从而对游标中 ...
- C#算法知识点记录
针对算法的知识点进行记录 简易桶排序 首先看一个简易桶排序,有一串数字,进行从大到小排列.数字间隔不大,使用一维数组来当作桶,进行插入排序. static void Main(string[] arg ...
- mysql 函数 GROUP_CONCAT 单元格中最长字符串和excel导出问题
GROUP_CONCAT 使用方式GROUP_CONCAT ([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符']) SELECT ...
- 一、Daily Scrum Meeting【Alpha】------Clover
[Alpha]Daily Scrum Meeting 第一次 [Alpha]Daily Scrum Meeting 第二次 [Alpha]Daily Scrum Meeting 第三次 [Alpha] ...
- <<< 如何查看自己是外网还是内网
判断的方法很简单,就是看你的网络中有没有路由器,不管是有线路由还是无线路由,只要你的网络中用了路由,那你就是内网,用路由器的网络有一个特点,那就是只要路由器在开着,那你开了电脑之后就可以直接上网,不需 ...
- 【原】理解javascript中的闭包
闭包在javascript来说是比较重要的概念,平时工作中也是用的比较多的一项技术.下来对其进行一个小小的总结 什么是闭包? 官方说法: 闭包是指有权访问另一个函数作用域中的变量的函数.创建闭包的常见 ...
- 在Application中集成Microsoft Translator服务之开发前准备
第一步:准备一个微软账号 要使用Microsoft Translator API需要在Microsoft Azure Marketplace(https://datamarket.azure.com/ ...
- 2016-03-04记录 H264.TXT 转成 H264.h264
H264.TXT文件 来源于板子上串口输出的数据,需要把该数据转成 *.h264用 H264的软件打开观察 txt中数据截图如下: MATLAB读入数据的代码: clc;close all;clear ...
- HTML页面去缓存
在页面中写入: 两种写法: 1. <META HTTP-EQUIV="nocache" CONTENT="no-cache"> 2. <HEA ...
- 如何删除PHP数组中的元素,并且索引重排(unset,array_splice)?
如果要在某个数组中删除一个元素,可以直接用的unset,但是数组的索引不会重排: <?php $arr = array('a','b','c','d'); unset($arr[1]); pri ...
Pandas是python的一个数据分析包,最初由AQR Capital Management于2008年4月开发,并于2009年底开源出来,目前由专注于Python数据包开发的PyData开发tea ...
学习Python过程中,发现Python没有Switch-case,过去写C习惯用Switch/Case语句,官方文档说通过if-elif实现.所以不妨自己来实现Switch-Case功能. 方法一 ...
可通过字典调用:{1:case1,2:case2}.get(x,lambda *args,**key:)() # 编写一个计算器 # -*- coding=utf-8 -*- def jia(x,y) ...
选择语句 if 条件判断 : # 条件可以加括号也可以不加括号 -- else: -- Python中没有switch语句这是可以使用if exp:.... elif exp:来代替 if 判断条件1 ...
namedtuple顾名思义,就是名字+元组的数据结构,下面就来看一下Python的collections模块中namedtuple结构使用示例 namedtuple 就是命名的 tuple,比较 ...
Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 假设要根据同学的名字查找对应的成绩 如果 ...
这篇文章主要介绍了Python中的高级数据结构详解,本文讲解了Collection.Array.Heapq.Bisect.Weakref.Copy以及Pprint这些数据结构的用法,需要的朋友可以参考 ...
logger 提供了应用程序可以直接使用的接口handler将(logger创建的)日志记录发送到合适的目的输出filter提供了细度设备来决定输出哪条日志记录formatter决定日志记录的最终输出 ...
1.内存.类型本质.连续存储 1.内存本质 2.C 语言实例-计算 int, float, double 和 char 字节大小 使用 sizeof 操作符计算int, float, double 和 ...
Mysql 视图 触发器 存储过程 游标 游标是从数据表中提取出来的数据,以临时表的形式存放在内存中,在游标中有一个数据指针,在初始状态下指向的是首记录,利用fetch语句可以移动该指针,从而对游标中 ...
针对算法的知识点进行记录 简易桶排序 首先看一个简易桶排序,有一串数字,进行从大到小排列.数字间隔不大,使用一维数组来当作桶,进行插入排序. static void Main(string[] arg ...
GROUP_CONCAT 使用方式GROUP_CONCAT ([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符']) SELECT ...
[Alpha]Daily Scrum Meeting 第一次 [Alpha]Daily Scrum Meeting 第二次 [Alpha]Daily Scrum Meeting 第三次 [Alpha] ...
判断的方法很简单,就是看你的网络中有没有路由器,不管是有线路由还是无线路由,只要你的网络中用了路由,那你就是内网,用路由器的网络有一个特点,那就是只要路由器在开着,那你开了电脑之后就可以直接上网,不需 ...
闭包在javascript来说是比较重要的概念,平时工作中也是用的比较多的一项技术.下来对其进行一个小小的总结 什么是闭包? 官方说法: 闭包是指有权访问另一个函数作用域中的变量的函数.创建闭包的常见 ...
第一步:准备一个微软账号 要使用Microsoft Translator API需要在Microsoft Azure Marketplace(https://datamarket.azure.com/ ...
H264.TXT文件 来源于板子上串口输出的数据,需要把该数据转成 *.h264用 H264的软件打开观察 txt中数据截图如下: MATLAB读入数据的代码: clc;close all;clear ...
在页面中写入: 两种写法: 1. <META HTTP-EQUIV="nocache" CONTENT="no-cache"> 2. <HEA ...
如果要在某个数组中删除一个元素,可以直接用的unset,但是数组的索引不会重排: <?php $arr = array('a','b','c','d'); unset($arr[1]); pri ...