python常用数据结构
0. 字典初始化
d = {‘a’:1,’b’:2}
或 d={}
d[‘a’] = 1
d[‘b’] = 2
是不是和json格式数据很相似,语法和JavaScript又很相似
1. 变量接受序列分解:
p = (3.14,8.23)
X,Y = p
print(x,y)
3.14,8.23
2. Collections.deque 固定长度队列。
>>> from collections import deque
>>> p = deque(maxlen = 2)
>>> p.append(3.14)
>>> p.append(8.23)
>>> p
deque([3.14, 8.23], maxlen=2)
3. heapq 最大值最小值。
>>> import heapq
>>> p = [3.14,8.23,7.10]
>>> heapq.nlargest(1,p)
[8.23]
>>> heapq.nsmallest(1,p)
[3.14]
5.找出两个字典相同键 和相同项
>>> from collections import OrderedDict
>>> d = {}
>>> d['a'] = 1
>>> d['c'] = 3
>>> d['b'] = 2
>>> dd = {}
>>> dd['a'] = 3
>>> dd['e'] = 5
>>> dd['b'] = 2
>>> d.keys() & dd.keys()
{'a', 'b'}
>>> d.items() & dd.items()
{('b', 2)}
6 去除数组或字典值重复的元素 set
>>> d ={1,1,2,2,3,3,4,5}
>>> set(d)
{1, 2, 3, 4, 5}
7 截取数组元素
>>> d = [1,1,2,2,3,3,4,5]
>>> d[2:4]
[2, 2]
>>> d[2:5]
[2, 2, 3]
8 统计元素出现次数Counter.most_common。
>>> d = [1,1,1,2,2,4,5]
>>> from collections import Counter
>>> master = Counter(d).most_common(2)
>>> print(master)
[(1, 3), (2, 2)]
4. collections.OrderedDict 有序字典
>>> from collections import OrderedDict
>>> d = OrderedDict()
>>> d['a'] = 1
>>> d['c'] = 3
>>> d['b'] = 2
>>> print(d)
OrderedDict([('a', 1), ('c', 3), ('b', 2)])
9.数组排序 operator.itemgetter
>>> from operator import itemgetter
>>> d = [2,1,5,4,3]
>>> sorted(d)
[1, 2, 3, 4, 5]
10.字典排序 sort
>>> from operator import itemgetter
>>> p = [{'x':'3','y':'3'},{'x':'4','y':'4'},{'x':'2','y':'2'},{'x':'1','y':'1'}]
>>> p.sort(key=itemgetter('x'))
>>> print(p)
[{'x': '1', 'y': '1'}, {'x': '2', 'y': '2'}, {'x': '3', 'y': '3'}, {'x': '4', 'y': '4'}]
11. lambda 表达式
>>> p = [{'x':'3','y':'3'},{'x':'4','y':'4'},{'x':'2','y':'2'},{'x':'1','y':'1'}]
>>> pLamb = [n['x'] > '2' for n in p]
>>> print(pLamb)
[True, True, False, False]
12 compress 与 11项 结合使用。
>>> from itertools import compress
>>> list(compress(p,pLamb))
[{'x': '3', 'y': '3'}, {'x': '4', 'y': '4'}]
13.逻辑上合并对象
>>> p = {'x':1,'y':2}
>>> p2 = {'y':3,'z':4}
>>> from collections import ChainMap
>>> c = ChainMap(p,p2)
>>> print(c)
ChainMap({'x': 1, 'y': 2}, {'y': 3, 'z': 4})
>>> list(c.keys())
['x', 'z', 'y']
python常用数据结构的更多相关文章
- python常用数据结构讲解
一:序列 在数学上,序列是被排成一排的对象,而在python中,序列是最基本的数据结构.它的主要特征为拥有索引,每个索引的元素是可迭代对象.都可以进行索引,切片,加,乘,检查成员等操作.在py ...
- Python常用数据结构之collections模块
Python数据结构常用模块:collections.heapq.operator.itertools collections collections是日常工作中的重点.高频模块,常用类型由: 计数器 ...
- Python常用数据结构之heapq模块
Python数据结构常用模块:collections.heapq.operator.itertools heapq 堆是一种特殊的树形结构,通常我们所说的堆的数据结构指的是完全二叉树,并且根节点的值小 ...
- python常用数据结构(1)
python中有四种最常用的数据结构,分别是列表(list),字典(dict),集合(set)和元组(tuple) 下面简单描述下它们的区别和联系 1.初始化 不得不说,python数据结构的初始化比 ...
- Python常用数据结构(列表)
Python中常用的数据结构有序列(如列表,元组,字符串),映射(如字典)以及集合(set),是主要的三类容器 内容 序列的基本概念 列表的概念和用法 元组的概念和用法 字典的概念和用法 各类型之间的 ...
- python 常用数据结构使用
python 字典操作 http://www.cnblogs.com/kaituorensheng/archive/2013/01/24/2875456.html python 字典排序 http:/ ...
- python常用数据结构的常用操作
作为基础练习吧.列表LIST,元组TUPLE,集合SET,字符串STRING等等,显示,增删,合并... #===========List===================== shoplist ...
- python 常用数据结构
#coding=utf- #元组,不可变序列(,) a=(,,,) print(a) a=tuple([,,,])#第二种定义方式 print(a) print(a[]) print(a[:]) #可 ...
- python常用数据结构(2)
1.有名字的元组——namedtuple >>> from collections import namedtuple >>> Point = namedtuple ...
- 学Python常用数据结构之字典
迄今为止,我们已经为大家介绍了Python中的三种容器型数据类型,但是这些数据类型还不足以帮助我们解决所有的问题.例如,我们要保存一个人的信息,包括姓名.年龄.体重.单位地址.家庭住址.本人手机号.紧 ...
随机推荐
- 张高兴的 Windows 10 IoT 开发笔记:HC-SR04 超声波测距模块
HC-SR04 采用 IO 触发测距.下面介绍一下其在 Windows 10 IoT Core 环境下的用法. 项目运行在 Raspberry Pi 2/3 上,使用 C# 进行编码. 1. 准备 H ...
- JavaScript正则表达式之分组匹配 / 反向引用
语法 元字符:(pattern) 作用:用于反复匹配的分组 属性$1~$9 如果它(们)存在,用于得到对应分组中匹配到的子串 \1或$1 用于匹配第一个分组中的内容 \2或$2 用于匹配第一个分组中的 ...
- 测试中出现ERROR StatusLogger No log4j2 configuration file
概述 在hibernate框架搭建完成用log4j2进行测试时,总是出现ERROR StatusLogger No log4j2 configuration file found. Using def ...
- Awesome Projects (汇聚全球所有🐮项目,你值得拥有)
Awesome Projects SkySeraph Oct 2017 Email:skyseraph00@163.com 更多精彩请直接访问SkySeraph个人站点:www.skyseraph.c ...
- apache+php+mysql运行环境
建议Apache2.4+php5.6+mysql5.5+phpmyadmin4.4.4 参考: http://jingyan.baidu.com/article/fcb5aff797ec41edaa4 ...
- java如何调用接口方式二
java如何调用接口 在实际开发过程中,我们经常需要调用对方提供的接口或测试自己写的接口是否合适,所以,问题来了,java如何调用接口?很多项目都会封装规定好本身项目的接口规范,所以大多数需要去调用对 ...
- python第四课——线程、进程、协程
面试or笔试题:简述线程.进程.协程之间的关系? 内容概要 1.进程与线程优.缺点的比较 2.适用情况 3.线程 线程的创建 setDaemon join event RLock 队列 4.进程 创建 ...
- LeetCode 665. Non-decreasing Array (不递减数组)
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- Spring MVC 快捷定义 ViewController
WHY : 为什么我们需要快捷定义 ViewController ? 在项目开发过程中,经常会涉及页面跳转问题,而且这个页面跳转没有任何业务逻辑过程,只是单纯的路由过程 ...
- 采访 Lua 发明人的一篇文章
采访 Lua 发明人的一篇文章 来源 https://blog.codingnow.com/2010/06/masterminds_of_programming_7_lua.html <Mast ...