您的位置:首页 > 编程语言 > Python开发

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]])
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 序列 基础