python 元祖,列表,字典 初识
2015-07-23 13:19
453 查看
#通用序列操作
所有序列类型都可以进行某些特定的操作。
这些操作包括:
索引(indexing):
分片(sliceing):
加(adding):不同数据,不同含义
乘(multiplying):
检查某个元素是否属于序列的成员(成员资格):
#列表:
#任意类型的对象位置的有序集合,没有固定的大小,其大小可变
#列表支持所有对序列的操作
DEMO1:
L=[1,2,3]
len(L) ###>>>3
#对列表进行索引,切片等操作
L[0] ###>>>1 indexing by position
L[:-1] ###>>>[1 ,2] Slicing a list returns a new list
L+[4,5,6] ###>>>[1,2,3,4,5,6] #Concatenation makes a new list
L ###>>>[1,2,3] not changing the original list
DEMO2:
#类型的特定操作
L= [1,'a',2.0]
L.append('NI') # add object at end of list
L ###>>> [1, 'a' , 2.0, 'NI']
L.pop(2) ###>>>2.0 delete item in the middle
L ###>>> [1, 'a' , 'NI']
####--------------------------------
pop()
del()
insert()
remove() 按值移除
本身列表可以改变,
sort() 升序排列
reverse() 列表进行反转
如果超出列表的引用范围,
error text omitted。。。
IndexError : list index out of range
IndexError : list assignment index out of range
DEMO3:
M = [
[1,2,3],
[4,5,6],
[7,8,9]
] # A 3X3 matrix as nested lists
# Code can span lines if bracketed
列表的解析表达式 (list comprehension expression)
对序列中的每一项运行表达式来创建一个新列表的方法
col2 = [row[1] for row in M]
col2 ###>>>[2,5,8]
[ row[1]+1 for row in M]
[ row[1] for row in M if row[1]%2 == 0 ]
list(map(sum,M)) ###>>>[6,15,24]
[ord(x) for x in 'spaam'] ###>>>[115,112,97,97,109]
{ord(x) for x in 'spaam'} ###>>>{112,97,115,109}
{x:ord(x) for x in 'spaam'} ###>>>{'a':97,'p':112,'s':115,'m':109}
#字典
不是一种序列,而是一种映射(mapping),通过键值对来存储,可变性
DEMO1:
D={'food':'Spam','quantity':4,'color':'pink'}
索引引用:
D['food'] ###>>>'Spam'
创建字典
(1)D={'food':'Spam','quantity':4,'color':'pink'}
(2)D={}
D['food']='Spam'
D['quantity']=4
D['color']='pink'
嵌套的字典
rec ={ 'name': {'first':'s','last':'titi'}
'job' : ['dev','mgr']
'age' : 40.5
}
rec['name'] ###>>> {'first':'s','last':'titi'}
rec['name']['first'] ###>>>'s'
rec['job'][-1] ###>>>'mgr'
rec['job'].append('janitor')
rec ###>>> rec = { 'name': {'first':'s','last':'titi'}
'job' : ['dev','mgr','janitor']
'age' : 40.5
}
字典中的列表拥有独立的内存
python中有一种垃圾回收的特性
键的排序:for循环
(1)
Ks=list(D.keys())
Ks.sort()
for key in Ks:
print key
(2)for key in sorted(D):
print key
迭代与优化:
squares = [x ** 2 for x in [1,2,3,4,5]]
[1,2,9,16,25]
列表解析和 相关的函数编程工具map filter比for循环快
if key in D:
if key not in D: ( 判断不存在的键 )
get()方法(带有一个默认值的条件索引)
value = D.get('x',0)
#元祖
T = (1,2,3,4)
len(T)
T+(5,6)
(1,2,3,4,5,6)
T[0] ###〉〉〉1
元祖的专用方法
T.index(4) ###>>> 3
T.count(4) ###>>> 1
T[0] = 2
元祖与列表,字典一样,支持混合类型,不可变性
所有序列类型都可以进行某些特定的操作。
这些操作包括:
索引(indexing):
分片(sliceing):
加(adding):不同数据,不同含义
乘(multiplying):
检查某个元素是否属于序列的成员(成员资格):
#列表:
#任意类型的对象位置的有序集合,没有固定的大小,其大小可变
#列表支持所有对序列的操作
DEMO1:
L=[1,2,3]
len(L) ###>>>3
#对列表进行索引,切片等操作
L[0] ###>>>1 indexing by position
L[:-1] ###>>>[1 ,2] Slicing a list returns a new list
L+[4,5,6] ###>>>[1,2,3,4,5,6] #Concatenation makes a new list
L ###>>>[1,2,3] not changing the original list
DEMO2:
#类型的特定操作
L= [1,'a',2.0]
L.append('NI') # add object at end of list
L ###>>> [1, 'a' , 2.0, 'NI']
L.pop(2) ###>>>2.0 delete item in the middle
L ###>>> [1, 'a' , 'NI']
####--------------------------------
pop()
del()
insert()
remove() 按值移除
本身列表可以改变,
sort() 升序排列
reverse() 列表进行反转
如果超出列表的引用范围,
error text omitted。。。
IndexError : list index out of range
IndexError : list assignment index out of range
DEMO3:
M = [
[1,2,3],
[4,5,6],
[7,8,9]
] # A 3X3 matrix as nested lists
# Code can span lines if bracketed
列表的解析表达式 (list comprehension expression)
对序列中的每一项运行表达式来创建一个新列表的方法
col2 = [row[1] for row in M]
col2 ###>>>[2,5,8]
[ row[1]+1 for row in M]
[ row[1] for row in M if row[1]%2 == 0 ]
list(map(sum,M)) ###>>>[6,15,24]
[ord(x) for x in 'spaam'] ###>>>[115,112,97,97,109]
{ord(x) for x in 'spaam'} ###>>>{112,97,115,109}
{x:ord(x) for x in 'spaam'} ###>>>{'a':97,'p':112,'s':115,'m':109}
#字典
不是一种序列,而是一种映射(mapping),通过键值对来存储,可变性
DEMO1:
D={'food':'Spam','quantity':4,'color':'pink'}
索引引用:
D['food'] ###>>>'Spam'
创建字典
(1)D={'food':'Spam','quantity':4,'color':'pink'}
(2)D={}
D['food']='Spam'
D['quantity']=4
D['color']='pink'
嵌套的字典
rec ={ 'name': {'first':'s','last':'titi'}
'job' : ['dev','mgr']
'age' : 40.5
}
rec['name'] ###>>> {'first':'s','last':'titi'}
rec['name']['first'] ###>>>'s'
rec['job'][-1] ###>>>'mgr'
rec['job'].append('janitor')
rec ###>>> rec = { 'name': {'first':'s','last':'titi'}
'job' : ['dev','mgr','janitor']
'age' : 40.5
}
字典中的列表拥有独立的内存
python中有一种垃圾回收的特性
键的排序:for循环
(1)
Ks=list(D.keys())
Ks.sort()
for key in Ks:
print key
(2)for key in sorted(D):
print key
迭代与优化:
squares = [x ** 2 for x in [1,2,3,4,5]]
[1,2,9,16,25]
列表解析和 相关的函数编程工具map filter比for循环快
if key in D:
if key not in D: ( 判断不存在的键 )
get()方法(带有一个默认值的条件索引)
value = D.get('x',0)
#元祖
T = (1,2,3,4)
len(T)
T+(5,6)
(1,2,3,4,5,6)
T[0] ###〉〉〉1
元祖的专用方法
T.index(4) ###>>> 3
T.count(4) ###>>> 1
T[0] = 2
元祖与列表,字典一样,支持混合类型,不可变性
相关文章推荐
- Python Technical Notes
- python的unittest测试框架的扩展浅谈
- 比较perl+python
- 在IDEA上用python来连接集群上的hive
- Python 深入理解yield
- python 机器学习库:Numpy
- Python脚本生成sitemap
- Python异常处理
- Python 正则表达式中的函数
- PYTHON风格规范-Google 开源项目风格指南
- PYTHON风格规范-Google 开源项目风格指南
- NumPy的详细教程
- 如何成为Python高手
- Python strip()函数
- python使用mysql数据库
- Python Base64转码解码
- Python Base64转码解码
- python写的批量操作远程主机脚本(命令执行,上传、下载文件)
- python 自己写爬虫 ---- 总结需要的包
- python 自己写爬虫 ----- BeautifulSoup