笔记-python-tutorial-5.data structure
笔记-python-tutorial-5.data structure
1. data structure
1.1. list operation
- list.append(x) #尾部添加一个元素
- list.extend(iterable)
- list.insert(I, x)
- list.remove(x)
remove the first item from the list whose value is x.if x is not exist return ValueError.
- list.pop([i])
- list.clear()
- list.index(x [, start, end]) #返回首个值为X的元素下标。
- list.count(x)
- list.sort(key=None, reverse=False)
- list.reverse()
- list.copy()
返回一个复制品而不是引用。
1.1.1. using lists as stacks
stack = [3,4,5]
stack.append(6)
stack.pop()
1.1.2. using lists as queues
列表如果使用先进先出就是队列了,当然这样没有效率,不符初衷。
from collections import deque
queue = deque([‘before’, ‘middle’, ‘after’])
queue.append(‘carry’)
queue.popleft()
1.1.3. 列表生成式
下面是使用函数常规列表生成:
squares = list(map(lambda x: x**2, range(10)))
等效于
squares = [x**2 for x in range(10)]
1.1.4. 复杂一点的嵌套式列表生成式
>>> matrix = [
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12],
... ]
>>> [[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
1.2. 删除del
a = [1,2,3,4,5]
del a[0]
del a[2:4]
del a[:]
del a
注意最后两个语句的效果是不一样的;
1.3. tuple
tuple是不可变的,但tuple中可以包含可变元素。
>>> t = ([45,67,3], [5,6,7,8],[54])
>>> t
([45, 67, 3], [5, 6, 7, 8], [54])
>>> t[1].append(879)
>>> t
([45, 67, 3], [5, 6, 7, 8, 879], [54])
>>> t[1] = [4]
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
t[1] = [4]
TypeError: 'tuple' object does not support item assignment
1.4. sets
python中的sets是一组无序不重复数据集合
类似于列表生成式,也支持生成式。
1.5. dictionaries
一些常规操作
>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'sape': 4139, 'guido': 4127, 'jack': 4098}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'guido': 4127, 'irv': 4127, 'jack': 4098}
>>> list(tel.keys())
['irv', 'guido', 'jack']
>>> sorted(tel.keys())
['guido', 'irv', 'jack']
>>> 'guido' in tel
True
>>> 'jack' not in tel
False
生成式
>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}
1.6. 元素遍历
对于字典有些不同,存在items()方法来同时访问键和值。
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
... print(k, v)
...
gallahad the pure
robin the brave
enumerate()序列访问,该方法会同时返回元素位置下标和值。
>>> for i, v in enumerate(['tic', 'tac', 'toe']):
... print(i, v)
...
0 tic
1 tac
2 toe
1.7. 条件判断
在while和if语句中可以使用带操作的条件判断式。
in and not in用于判断一个值是否存在于序列中。is用于判断两个对象是否是同一对象,需要注意区别。
a<b==c是合规的比较式,等于a<b and b ==c
and or 是可以在比较式中使用的,注意写法,简写一时爽,回看爽过头。
1.8. 序列之间比较
相同类型的序列之间可以比较。
比较原则:
- 先比较前面的元素,如果不同则给出结果;
- 如果相同,则按序逐个比较;
- 如果所有元素相等,则序列相等;
- 如果某一序列是另一序列的初始子序列,则较短的序列小;
- 字符串比较使用unicode编号来比较。
(1, 2, 3) < (1, 2, 4)
[1, 2, 3] < [1, 2, 4]
'ABC' < 'C' < 'Pascal' < 'Python'
(1, 2, 3, 4) < (1, 2, 4)
(1, 2) < (1, 2, -1)
(1, 2, 3) == (1.0, 2.0, 3.0)
(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
笔记-python-tutorial-5.data structure的更多相关文章
- [译]The Python Tutorial#5. Data Structures
[译]The Python Tutorial#Data Structures 5.1 Data Structures 本章节详细介绍之前介绍过的一些内容,并且也会介绍一些新的内容. 5.1 More ...
- FAT文件系统规范v1.03学习笔记---2.FAT区之FAT数据结构(Fat Data Structure)
1.前言 本文主要是对Microsoft Extensible Firmware Initiative FAT32 File System Specification中文翻译版的学习笔记. 每个FAT ...
- Python: tree data structure
# 树结构 from pythonds.basic.stack import Stack #pip install pythonds from pythonds.trees.binaryTree im ...
- Python Tutorial笔记
Python Tutorial笔记 Python入门指南 中文版及官方英文链接: Python入门指南 (3.5.2) http://www.pythondoc.com/pythontutorial3 ...
- LeetCode 笔记27 Two Sum III - Data structure design
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- Python Tutorial 学习(六)--Modules
6. Modules 当你退出Python的shell模式然后又重新进入的时候,之前定义的变量,函数等都会没有了. 因此, 推荐的做法是将这些东西写入文件,并在适当的时候调用获取他们. 这就是为人所知 ...
- 【转】Multithreaded Python Tutorial with the “Threadworms” Demo
The code for this tutorial can be downloaded here: threadworms.py or from GitHub. This code works wi ...
- 211. Add and Search Word - Data structure design
题目: Design a data structure that supports the following two operations: void addWord(word) bool sear ...
- Python Tutorial学习(十一)-- Brief Tour of the Standard Library – Part II
11.1. Output Formatting 格式化输出 The repr module provides a version of repr() customized for abbreviate ...
随机推荐
- Elasticsearch在后台启动
Elasticsearch在linux下使用命令sh elasticsearch start,按键ctrl+c的时候程序就会stop掉,如何将程序在后台启动呢? 需要使用:./elasticsearc ...
- 处理 wait millis 60009, active 50 ,maxactive 200 异常 过程
处理 wait millis 60009, active 50 ,maxactive 200 异常 过程2018年04月19日 16:48:46 守望dfdfdf 阅读数:1910 标签: druid ...
- SpringBoot的特性
SpringBoot的理念“习惯优于配置” 习惯优于配置(项目中存在大量的配置,此外还内置了一个习惯性的配置,无须手动进行配置) 使用SpringBoot可以方便地创建独立运行.准生产级别的基于Spr ...
- ansible 2.1.0 api 编程
pdf文档 https://media.readthedocs.org/pdf/ansible/latest/ansible.pdf api介绍 http://blog.csdn.net/python ...
- jquery UI_tabs
1.tab使用 <!doctype html> <html lang="en"> <head> <meta charset="u ...
- bootstrap table保留多选框的分页
有时候需要完成这种情况: 1.需要设置的是如果第一页点击复选框然后点击其他页面的话,第一页的选项被保存了 2.将所有选择好的复选款的数据保存在数组中 bootstrap table官方文档http:/ ...
- 流媒体 8——因特网 tcp/ip
1 因特网 1.1 因特网的结构 组成因特网的子网之间在物理上的相互连接都是通过网关设备实现的.通过网关设备互相连接在一起的不同的网络通常称为子网 (subnetwork),因为它们是大网络之中的网络 ...
- linux 命令——10 cat (转)
cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1.命令格式: cat [选项] [文件] ...
- java—三大框架详解,其发展过程及掌握的Java技术慨括
Struts.Hibernate和Spring是我们Java开发中的常用关键,他们分别针对不同的应用场景给出最合适的解决方案.但你是否知道,这些知名框架最初是怎样产生的? 我们知道,传统的Java W ...
- 【洛谷1337】[JSOI2004] 吊打XXX(模拟退火经典题)
点此看题面 大致题意: 一个平面上有\(n\)个点,每个点有1个权值,现在要选择平面上的一个点,使这\(n\)个点的权值乘上到达选定点的距离之和最小. 模拟退火 我们可以用模拟退火来做这道题. 先将\ ...