博客
关于我
python内置数据结构性能分析
阅读量:446 次
发布时间:2019-03-06

本文共 507 字,大约阅读时间需要 1 分钟。

List

  1. 列表是一个类似数组的结构
  2. 对某个索引位置的获取和赋值的时间复杂度是O(1)
  3. 在列表末尾添加或删除一个元素的时间复杂度是O(1)
  4. 但是在列表除末尾之外的位置添加或删除一个元素的时间复杂度是O(n)。这是因为,在选定位置之后的元素需要被整体前移或后移
  5. 列表搜索的时间复杂度是O(n),因为列表搜索是靠遍历元素完成的
  6. 列表的extend时间复杂度是O(k),相当于在列表末尾插入k个元素
  7. 列表的sort函数内部实现机制为timesort,时间复杂度为O(nlogn),空间复杂度为O(n),是一种稳定的排序方法

Dict

  1. 字典的底层实现是哈希表
  2. 字典是无序的,但可以使用collections.OrderedDict()实现有序字典。OrderedDict的实现原理是哈希表+双链表
  3. 字典的增删改查都是O(1)的时间复杂度

Set

  1. 集合的底层实现是哈希表
  2. 集合的增删改查与字典一样,都是O(1)的时间复杂度

collections.deque

  1. 双向队列的底层实现是双链表
  2. 一种对列表的改进:在头尾的增删都是O(1)的时间复杂度
  3. 但是在队列中的增删仍是O(n)的时间复杂度
  4. 查找也是O(n)的时间复杂度

参考资料

转载地址:http://wylyz.baihongyu.com/

你可能感兴趣的文章
Nmap扫描教程之Nmap基础知识
查看>>
nmap指纹识别要点以及又快又准之方法
查看>>
Nmap渗透测试指南之指纹识别与探测、伺机而动
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>