python 之序列学习笔记
2013-08-11 18:56
351 查看
一、 翻转序列
>>>s="abcdef"
>>>s[::-1]
"fedcba"
二、各一个取一个
>>>s[::2]
"ace"
三、None索引值
>>>s="abcde"
>>>for i in [None] +range(-1,-len(s),-1):
``` print s[:i]
```
abcde
abcd
abc
ab
a
四、序列常函数
len(s) max(s) min(s) reversed(s) sorted(s) sum(s)
五、字符串模板
>>>from string import Template
>>>s = Template('There are ${howmany} ${lang} quotation Sysmbols')
>>>print s.safe_substitute(lang='python',howmany=3)
>>>print s.substitute(lang='python',howmany=3)
六、 寻找原始的\n字符二不是换行
>>>import re
>>>m = re.search(r'\\[rtfvn]',r'hello world!\n')
>>>if m is not None:m.group()
```'
'\\n'
七、字符串内嵌函数
cmp() 返回0 表示相等
len() max() min()
enumerate() 例子:
>>>s = "foobar"
>>>for i,t in enumerate(s):
``` print i , t
```
0 f
1 o
2 o
3 b
4 a
5 r
zip() 例子:
>>>s,t='foa','obr'
>>>zip(s,t)
[('f','o'),('o','b'),('a','r')]
七、字符串类型函数
raw_input() str() unicode()
char() unichr() ord()
八、字符串的不可变形
不要尝试的修改字符串中的某个字符,这是不允许的,如:
>>>s = "abcde"
>>>s[2] = "q"
这时候会提示错误
假设要修改,请使用:
>>>s="%sQ%s" % (s[0:2],s[3:])
九、列表
创建列表、访问列表并更新列表,还可以用list()函数创建;
>>> aList = [123,'abc',4.56,['inner','list'],7-9j]
>>> aList
[123, 'abc', 4.5599999999999996, ['inner', 'list'], (7-9j)]
>>> aList[1:4]
['abc', 4.5599999999999996, ['inner', 'list']]
>>> aList[3][1]
'list'
>>> aList[2] = "hello-shit"
>>> aList
[123, 'abc', 'hello-shit', ['inner', 'list'], (7-9j)]
删除列表元素、删除列表:
>>> del aList[1]
>>> aList
[123, 'hello-shit', ['inner', 'list'], (7-9j)]
>>> aList.remove(123)
>>> aList
['hello-shit', ['inner', 'list'], (7-9j)]
>>> aList.pop()
(7-9j)
>>> aList
['hello-shit', ['inner', 'list']]
>>> del aList
>>> aList
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'aList' is not defined
>>>
十、连接操作符 和 extend()、append()
连接操作符必须两边的类型一致,且创建的是一个新的列表;
extend()实际上是把新列表添加到原有列表上;
append()给列表添加新元素
十一、列表与for混合的赋值例子
>>> [i * 2 for i in [8,-2,5]]
[16, -4, 10]
>>> [i for i in range(8) if i % 2 == 0]
[0, 2, 4, 6]
>>>
十二、查看python类型的方法和属性,可用dir(参数),例子:dir(list)
十三、元组与列表
相同点:赋值、更新、删除等内嵌函数都可以参考列表;
不同点:元组有不可变性
单元素元组需要加逗号:(‘abc’,),单元素列表不用:['abc'];
但是,实际上,元组也不是那么“不可变”:
1. 连接操作符可用
>>> s ="first"
>>> s = s + "second"
>>> s
'firstsecond'
2. 元组内的列表可用
>>> t = (["xyz","abc"],"123","sdf")
>>> t
(['xyz', 'abc'], '123', 'sdf')
>>> t[0][1]
'abc'
>>> t[0][1]=["go","do"]
>>> t
(['xyz', ['go', 'do']], '123', 'sdf')
>>>
3. 元组可重复操作
>>> t
(['xyz', ['go', 'do']], '123', 'sdf')
>>> t = t + (['xyz', ['go', 'do']], '123', 'sdf')
>>> t
(['xyz', ['go', 'do']], '123', 'sdf', ['xyz', ['go', 'do']], '123', 'sdf')
十四、浅拷贝、深拷贝
>>> person = ['name',['savings',100.00]]
>>> hubby = person[:]
>>> wifey = list(person)
>>> [id(x) for x in person ,hubby,wifey]
[47841970771728, 47841970773672, 47841970773960]
>>> hubby[0]='joe'
>>> wifey[0]='jane'
>>> hubby,wifey
(['joe', ['savings', 100.0]], ['jane', ['savings', 100.0]])
>>> hubby[1][1]
100.0
>>> hubby[1][1] = 50.00
>>> hubby,wifey
(['joe', ['savings', 50.0]], ['jane', ['savings', 50.0]])
使用deepcopy()后,可下:
>>> import copy
>>> wifey = copy.deepcopy(person)
>>> wifey
['name', ['savings', 50.0]]
>>> hubby[1][1]
50.0
>>> hubby[1][1]=60.00
>>> hubby,wifey
(['joe', ['savings', 60.0]], ['name', ['savings', 50.0]])
>>>s="abcdef"
>>>s[::-1]
"fedcba"
二、各一个取一个
>>>s[::2]
"ace"
三、None索引值
>>>s="abcde"
>>>for i in [None] +range(-1,-len(s),-1):
``` print s[:i]
```
abcde
abcd
abc
ab
a
四、序列常函数
len(s) max(s) min(s) reversed(s) sorted(s) sum(s)
五、字符串模板
>>>from string import Template
>>>s = Template('There are ${howmany} ${lang} quotation Sysmbols')
>>>print s.safe_substitute(lang='python',howmany=3)
>>>print s.substitute(lang='python',howmany=3)
六、 寻找原始的\n字符二不是换行
>>>import re
>>>m = re.search(r'\\[rtfvn]',r'hello world!\n')
>>>if m is not None:m.group()
```'
'\\n'
七、字符串内嵌函数
cmp() 返回0 表示相等
len() max() min()
enumerate() 例子:
>>>s = "foobar"
>>>for i,t in enumerate(s):
``` print i , t
```
0 f
1 o
2 o
3 b
4 a
5 r
zip() 例子:
>>>s,t='foa','obr'
>>>zip(s,t)
[('f','o'),('o','b'),('a','r')]
七、字符串类型函数
raw_input() str() unicode()
char() unichr() ord()
八、字符串的不可变形
不要尝试的修改字符串中的某个字符,这是不允许的,如:
>>>s = "abcde"
>>>s[2] = "q"
这时候会提示错误
假设要修改,请使用:
>>>s="%sQ%s" % (s[0:2],s[3:])
九、列表
创建列表、访问列表并更新列表,还可以用list()函数创建;
>>> aList = [123,'abc',4.56,['inner','list'],7-9j]
>>> aList
[123, 'abc', 4.5599999999999996, ['inner', 'list'], (7-9j)]
>>> aList[1:4]
['abc', 4.5599999999999996, ['inner', 'list']]
>>> aList[3][1]
'list'
>>> aList[2] = "hello-shit"
>>> aList
[123, 'abc', 'hello-shit', ['inner', 'list'], (7-9j)]
删除列表元素、删除列表:
>>> del aList[1]
>>> aList
[123, 'hello-shit', ['inner', 'list'], (7-9j)]
>>> aList.remove(123)
>>> aList
['hello-shit', ['inner', 'list'], (7-9j)]
>>> aList.pop()
(7-9j)
>>> aList
['hello-shit', ['inner', 'list']]
>>> del aList
>>> aList
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'aList' is not defined
>>>
十、连接操作符 和 extend()、append()
连接操作符必须两边的类型一致,且创建的是一个新的列表;
extend()实际上是把新列表添加到原有列表上;
append()给列表添加新元素
十一、列表与for混合的赋值例子
>>> [i * 2 for i in [8,-2,5]]
[16, -4, 10]
>>> [i for i in range(8) if i % 2 == 0]
[0, 2, 4, 6]
>>>
十二、查看python类型的方法和属性,可用dir(参数),例子:dir(list)
十三、元组与列表
相同点:赋值、更新、删除等内嵌函数都可以参考列表;
不同点:元组有不可变性
单元素元组需要加逗号:(‘abc’,),单元素列表不用:['abc'];
但是,实际上,元组也不是那么“不可变”:
1. 连接操作符可用
>>> s ="first"
>>> s = s + "second"
>>> s
'firstsecond'
2. 元组内的列表可用
>>> t = (["xyz","abc"],"123","sdf")
>>> t
(['xyz', 'abc'], '123', 'sdf')
>>> t[0][1]
'abc'
>>> t[0][1]=["go","do"]
>>> t
(['xyz', ['go', 'do']], '123', 'sdf')
>>>
3. 元组可重复操作
>>> t
(['xyz', ['go', 'do']], '123', 'sdf')
>>> t = t + (['xyz', ['go', 'do']], '123', 'sdf')
>>> t
(['xyz', ['go', 'do']], '123', 'sdf', ['xyz', ['go', 'do']], '123', 'sdf')
十四、浅拷贝、深拷贝
>>> person = ['name',['savings',100.00]]
>>> hubby = person[:]
>>> wifey = list(person)
>>> [id(x) for x in person ,hubby,wifey]
[47841970771728, 47841970773672, 47841970773960]
>>> hubby[0]='joe'
>>> wifey[0]='jane'
>>> hubby,wifey
(['joe', ['savings', 100.0]], ['jane', ['savings', 100.0]])
>>> hubby[1][1]
100.0
>>> hubby[1][1] = 50.00
>>> hubby,wifey
(['joe', ['savings', 50.0]], ['jane', ['savings', 50.0]])
使用deepcopy()后,可下:
>>> import copy
>>> wifey = copy.deepcopy(person)
>>> wifey
['name', ['savings', 50.0]]
>>> hubby[1][1]
50.0
>>> hubby[1][1]=60.00
>>> hubby,wifey
(['joe', ['savings', 60.0]], ['name', ['savings', 50.0]])
相关文章推荐
- Python学习笔记——sorted()对tuple类型的序列排序
- python序列学习笔记
- 【Python学习笔记】4.序列
- python学习笔记(2)序列的通用基本操作
- Python学习笔记----序列共性
- python学习笔记19(序列的方法)
- Python学习笔记之-sequence序列
- Python学习笔记——元组,序列
- Python学习笔记 -- 序列(一)序列类型
- python学习笔记19(序列的方法)
- Python学习笔记2_序列
- Python笔记1--关于序列的学习
- python 学习笔记5------序列
- Python学习笔记 -- 序列(四)元组
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(3) 最大子序列和问题
- 简明python教程学习笔记之九-序列的通用操作(索引和切片)
- 【Python】学习笔记三:序列
- python学习笔记(1-4)- 数据结构序列之字符串
- python基础教程_学习笔记1:序列-1
- Python学习笔记(六)—— 序列运算