Python data structure
来源:互联网 发布:c语言中auto是什么意思 编辑:程序博客网 时间:2024/05/01 09:26
1. append(x) = > a[len(a):] = [x],
extend(L) => a[len(a):] = L,
insert(i,x) # before i,
remove(x) # remove x or return error,
pop([i]) # default value is the last element, and return that.
count(x) # count the appearance times
sort()
reverse()
2. as a stack
a.append(),a.pop() => push,pop
3. as a queue
a.append(),a.pop(0)=>push_back,pop_front
4. filter(function,sequence)=> each item call function, if that function return true,then the answer include this item
map(function,sequence)=> each item call function, if that is true then add to the answer linked list
map(None,list1,list2) => make two list to be a couple
reduce(function,sequence,[initial val]) => first two item call this function,and then call the function with returned item and the next item,and to the end, when there is only on item then return itself, if sequence is none then throw a exception except there is a initial value
5. derive list
vec = [2,4,5]
[x*3 for x in vec if x>3]
[[x,x**3] for x in vec]
[(x,x**3) for x in vec] # tuples
[x*y for x in vec1 for y in vec2]
[x+y for x in vec1 for y in vec2]
[vec1[i]*vec2[i] for i in range(len(vec1))]
6. del
del a[0]
del a[2:4] # third and forth item
del a
7. tuples
tuple must be nested and can not be changed like string
empty tuple: empty = (), singleton = 'hello', # comma is needed
inverse operation: t = 233,'dasfd',323 x,y,z = t
8. dictionary
associate memories, associate arrays
key:values
t = {'man':0,'woman':1,'child':2}
t['man'],t.has_key('woman') return true
construct a dictionary use list tuple
dict([(x,x*x) for x in vec])
traverse a dictionary
for k,v in t.items():
print k,v
9. use sequence
for i,v in enumerate(['adfd','dklfj','sdf']):
print i,v
10. two or more sequence
for q,a in zip(questions,answers):
print 'what is your %s, it is %s.' %(q,a)
11. compare operation can pass
a < b == c (a<b && b==c)
not > and > or
A and not B or C(A & !B or C)
string1, string2,string3 = '','abc','def'
non_null = string1 or string2 or string3
12. compare
(1,2,3) < (1,2,4)
[1,2,3] < (1,2,4)
'abc'<'c'<'d'
(1,2,3,4) < (2)
(1,2) < (1,2,-1)
(1,2,3) == (1.0,2.0,3.0)
can compare different types,
list < string < tuple
- data structure in python
- Python data structure
- python data structure
- Python 3.3 Tutorial Notes - 3:Data Structure
- [Python for Data Analysis]Python Basic2--Data Structure and Sequences
- Data Structure
- Data structure
- Data Structure
- Data structure
- Data structure
- data structure
- Data Structure
- Data structure
- Data Structure
- Data Structure
- Data Structure
- Data Structure
- Data structure alignmentData structure alignment
- Hadoop企业级完整训练:HDFS&MapReduce&HBase&Hive&Zookeeper&Pig&Project)
- ubuntu配置skynet
- Apache shiro(2)—first Demo(web+spring+shiro)
- 认识java.security.MessageDigest 类
- 10米USB延长线制作方法
- Python data structure
- HTML5三屏合一的架构设计
- 杂
- 随机取数组,打乱数组元素
- IE浏览器和火狐浏览器兼容问题——CSS篇
- WAS 各版本比较(5、6、7)
- SAP MM模块-组织结构第二篇
- Quartz 2D 编程指南(一)概述
- python模块-系统模块sys