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

python序列:字符串,元组和列表

2012-12-17 18:38 633 查看
一些python类型,它们的成员有是有序排列的,可以通过下标偏移量访问到它的一个或者向个成员,这类python类型称之为序列,包括字符串(普通字符串和unicode字符串),列表和元组

字符串的访问小例子

>>> a='this is a test'

>>> for b in range(len(a)): 下标偏移量是从0开始到总元素-1结束

... print a

...

t

h

i

s

i

s

a

t

e

s

t

>>>

>>> a='abcde'

>>> a[3]=3

Traceback (most recent call last):

File "<stdin>", line 1, in ?

[b]TypeError: object does not support item assignment
字符串不支持修改,因为字符串是不可修改类型呀


成员关系操作符in / not in 判断一个元素是否属于一个序列

>>> a="this is a test"

>>> if 'is' in a:

... print 'get it'

...

get it

连接操作符号+

重复操作符*

切片操作符[],[:][::]

>>> name=('peter','sz','isc','ibm')

>>> print name[4]

Traceback (most recent call last):

File "<stdin>", line 1, in ?

IndexError: tuple index out of range <======索引值超出范围,这个问题的原因是name元组只有4个元素,

>>>

>>> name=('peter','sz','isc','ibm')

>>> print name[0:2] 开始索引与结束索引,但不包括结束索引对应的元素

('peter', 'sz')

>>> print name[:2]

('peter', 'sz')

>>>

如果没有指定开始或者结束的索引值,则默认是从最开始处开始,到最末尾处结束

>>> name=('peter','sz','isc','ibm')

>>> print name[0:2]

('peter', 'sz')

>>> print name[:2]

('peter', 'sz')

>>> print name[2:3]

('isc',)

>>> print name[2:4]

('isc', 'ibm')

>>> print name[2:]

('isc', 'ibm')

>>> print name[:] 如果要访问整个序列,可以不指定开始和结束的索引,或者直接用序列的名称代替

('peter', 'sz', 'isc', 'ibm')

>>> print name

('peter', 'sz', 'isc', 'ibm')

>>> a="a1b2c3d4e5f6g7h8i9"

>>> a[::-1] 第三个参数是步长,如-1相当于"翻转"操作

'9i8h7g6f5e4d3c2b1a'

>>> a[::2] 2相当于隔一个取一个的操作

'abcdefghi'

>>> a[::-1][::2]

'987654321'

>>> a[:100] 切片的一个好处是索引值可以超过字符串的长度

'a1b2c3d4e5f6g7h8i9'

list(),str(),tuple()将对象进行转换,unicode()是str()函数的unicode版本

>>> a="a1b2c3d4e5f6g7h8i9"

>>> type(a)

<type 'str'>

>>> b=list(a)

>>> type(b)

<type 'list'>

>>>

>>> s = ['abc', 'This is a test', 'Hello, Python']

>>> for i, line in enumerate(s): enumerate()函数返回一个可迭代对象的索引值和对应的元素

... print i+1,
line

...

1 abc

2 This is a test

3 Hello, Python

max()返回序列中的最大值,mix返回序列中的最小值,reveresed()返回一个逆序的序列

>>> b =reversed(a)

>>> type(b)

<type 'listreverseiterator'>

>>> for d in reversed(a):

... print d

...

4

3

2

1

>>> a

[1, 2, 3, 4]

string模块预定义的字符串

>>> import string

>>> string.uppercase

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

>>> string.lowercase

'abcdefghijklmnopqrstuvwxyz'

>>> string.digits

'0123456789'

>>> string.letters

'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

如用于判断python有效标识符(如变量名的时候)使用成员关系(in/ not in)来判断会用到上面的定义,但也可以自己定义这些内容呵(个人不建议使用string模块的)

大小写字母的转换方法

>>> a='thisis a test'

>>> a.upper() upper()与lower函数将字母进行大小写转换

'THISIS A TEST'

>>> b=a.upper()

>>> c=b.lower()

>>> c

'thisis a test'

>>>

>>> a=56789.2343243434

>>> print "number is %5.5f" %a %5.5f说明是浮眯数,5.5说明带5位小数,整数部分按实际输出

number is 56789.23432

>>> a=56789123.2343243434

>>> print "number is %5.5f" %a

number is 56789123.23432

>>> a=56.2343243434

>>> print "number is %5.5f" %a

number is 56.23432

字符串到数字的转换,sting的函数

FUNCTIONS

atof(s)

atof(s) -> float

Return the floating point number represented by the string s.

atoi(s, base=10)

atoi(s [,base]) -> int

Return the integer represented by the string s in the given

base, which defaults to 10. The string s must consist of one

or more digits, possibly preceded by a sign. If base is 0, it

is chosen from the leading characters of s, 0 for octal, 0x or

0X for hexadecimal. If base is 16, a preceding 0x or 0X is

accepted.

atol(s, base=10)

atol(s [,base]) -> long

Return the long integer represented by the string s in the

given base, which defaults to 10. The string s must consist

of one or more digits, possibly preceded by a sign. If base

is 0, it is chosen from the leading characters of s, 0 for

原始字符串操作符r/R

用法,将字符串用引号括起来,在第一个引号前加一个r/R字母,常用于windows平台,如c:\test1\test等, 因为\t代表tab键,如果用了r'c:\test1\test'则不会将\t误解为tab

>>> a='ABCDEFG'

>>> b='1234567'

>>> c='abcdefg'

>>> zip(a,b,c)

[('A', '1', 'a'), ('B', '2', 'b'), ('C', '3', 'c'), ('D', '4', 'd'), ('E', '5', 'e'), ('F', '6', 'f'), ('G', '7', 'g')]

>>> for a,b,c in zip(a,b,c):

... print a,b,c

...

A 1 a

B 2 b

C 3 c

D 4 d

E 5 e

F 6 f

G 7 g

zip()函数取参数中的a[0],b[0],c[0]构成为一个元组,作为返回的第一个参数,依次类推,如果元素个数不等,则只返回各个参数都有的部分

>>> a='ABCDEFG'

>>> b='1234567'

>>> c='abc'

>>> for a,b,c in zip(a,b,c):

... print a,b,c

...

A 1 a

B 2 b

C 3 c

上面的例子中没有显示a[4]............


字符串内建函数(很重要呵!)

>>> question="what is your favorist color?"

>>> question.capitalize() <===首字母大写

'What
is your favorist color?'

>>>

>>> question.upper()

'WHAT IS YOUR FAVORIST COLOR?'

>>> question.center(100)

' what is your favorist color? '
<=====按100的宽度将字符串具中显示

>>> question.count(or)

File "<stdin>", line 1

question.count(or)

^

SyntaxError: invalid syntax

>>> question.count('or') <=查字符串中or子中出现的次数

2

>>> question.count('your')

1

>>> question.endswith('color?')

True

>>> question.endswith('color') 判断字符串是以什么结束的

False

>>> question.endswith('r?')

True

>>> question.find('test')

-1

>>> question.find('your') 字符串是否有子串"your"有则返回最近的索引值,否则返回-1(常用呵,很重要的)

8

>>> question.find('w')

0

>>> question.index('w')

0

>>> question.index('your')

8

>>> question.index('our')

9

>>> question.index('ur')

10

>>> question.index('mour') index与find类似,只不过find找不到返回-1.而index则会报错

Traceback (most recent call last):

File "<stdin>", line 1, in ?

ValueError: substring not found

>>> question.find('wooo')

-1

>>>

>>> question.rfind('or') rfind与find是一样的,不过它是从右边开始查起来的

25

>>> question.find('or')

16

>>> question

'what is your favorist color?'

>>> question.ljust(60)

'what is your favorist color? ' 返回的字符串长度变了成了一个新的字符串(注意单引号的变化),ljust是左对齐

>>> question.rjust(60)

' what is your favorist color?' rjust()是右对齐

>>> question.zfill(60)

'00000000000000000000000000000000what is your favorist color?' zfill(60)将原字符串右对齐,不足的用000填充

>>>

>>> c='What Is You Love'

>>> c.istitle() "标题化"是将所有单词的首写字母大写,其余小写,可以用title()函数来实现

True

>>> question.title()

'What Is Your Favorist Color?'

>>> a='this is a good chance' a是一个字符串的时候,join函数的表达

>>> ':'.join(a)

't:h:i:s: :i:s: :a: :g:o:o:d: :c:h:a:n:c:e'

>>> a=('this','is','a','good', 'chance') a是一个元组的时候

>>> ':'.join(a)

'this:is:a:good:chance'

join函数用string将各个元素连接起来,在上面的例子中string是: (冒号)

string.lstrip()

string.rstrip()

上面用于删除左右两边的空格

>>> a=' this is a test ' 字符串左右两边都有空格

>>> a.lstrip()

'this is a test '

>>> a.rstrip()

' this is a test'

>>>

>>> a.strip() 直接删除对象左右两边的空格

'this is a test'

>>> a='this is a test'

>>> a.replace('is','eeeeeee') replace()用指定字符串替换字符串里的子串(在这里子串是is)

'theeeeeee eeeeeee a test'

>>>

>>> a='this is a test'

>>> b=a.split() split()函数将一个字符串分隔为一个列表

>>> type(b)

<type 'list'>

>>> b

['this', 'is', 'a', 'test']

如用在用户密码文件时候用

>>> a='this is a test'

>>> a.startswith('this')

True

>>> a.startswith('is') 相对于endswith(),用于判断字符串是以什么开头的

False

>>>

>>> a='this IS A test'

>>> a.swapcase() swapcase()翻转字符串中的大小写

'THIS is a TEST'

列表list

>>> a=[] 空列表

>>> b=[1,2,3,4,5,6,7] 列表赋值

>>> c=[1,2,3,[4,5,6]] 列表嵌套

>>> print c[3] 列表的访问与字符串中元素访问一样

[4, 5, 6]

>>> print c[2]

3

>>>

>>> d=b[1:3] 列表的截取

>>> print d

[2, 3]

>>>

列表可以容纳任意数量的python对象,对象不要求都是一个类型,

列表元素的更新(修改值和添加元素)

>>> d[0]='abc' =修改某一个元素的值

>>> print d

['abc', 3]

>>> d.append('test') 追加一个元素到列表中

>>> d

['abc', 3, 'test']

列表的比较也是用内建的cmp参数,比较逻辑是按两个列表的元素分别比较,直到有一方胜出为止

>>> a=['abc','123','test']

>>> b=['abc','132','abce']

>>> a > b

False

>>> d=['abc','112','abce']

>>> a >d

True

>>> cmp(a,b)

-1

>>> cmp(a,d)

1

>>>

成员关系操作符

in /not in

>>> a=['abc','123','test']

>>> 'a' in a

False

>>> 'abc' in a

True

>>> 'a' not in a

True

>>>

连接操作符

>>> a=['abc','123','test']

>>> b=[1,2,3,4,5]

>>> a+b =连接操作符并没有改变原有的列表,而是创建了一个新的列表

['abc', '123', 'test', 1, 2, 3, 4, 5]

>>> a

['abc', '123', 'test']

>>> b

[1, 2, 3, 4, 5]

>>> a.extend(b) extend()扩展为更表扩展新元素

>>> a

['abc', '123', 'test', 1, 2, 3, 4, 5]

>>> b+'123' 因为b是列表,而'123'是一个字符串,因此不支持用连接操作符的

Traceback (most recent call last):

File "<stdin>", line 1, in ?

TypeError: can only concatenate list (not "str") to list

*重复操作符

>>> a='*'

>>> a*50

'**************************************************'

>>> a*60

'************************************************************'

>>> [i * 2 for i in [1,2,3,7,8,9]] 这些叫列表解析

[2, 4, 6, 14, 16, 18]

>>>

>>> [i for i in range(8) if i % 2 ==0]

[0, 2, 4, 6]

>>> a=[1,2,3]

>>> b=['1','2','3'] :原因是因为字母比数字大

>>> a > b

False

>>>

列表比较原则,1: 同类型则比较其值,上面的元素一个是数字,一个是字符串,不同类型

2:如果是数字,执行必要的数学转换再比较

3: 一方是数字,则另一方大,(数字在ascii中是取小的呵!

4:否则,通过类型名称的字母顺序来比较,

5,元素数目多的元素大

>>> b=['1','2','3']

>>> c=['1','2','3',-1]

>>> b >c 因为C的元素数目多

False

>>>

>>> a=[9999999]

>>> b=['i']

>>> type(a[0])

<type 'int'>

>>> type(b[0])

<type 'str'>

>>> a > b 因为str > int

False

>>> c=['1','2','3',-1]

>>> len(c) len()返回列表素的个数

4

max(),min()返回最大最小值,通常用于同类型对象的比较,

reversed()返回逆序的列表

>>> a=['there','they','them','xyz']

>>> b=reversed(a)

>>> b

<listreverseiterator object at 0x2ba3b77fd6d0>

>>> for a in reversed(a):

... print a

...

xyz

them

they

there

内置的reversed函数并不是返回一个已经造好的string,而是一个可以进行looping或者传送以调用例如.join等‘累加器’性质的方法的迭代器

class reversed(object)

| reversed(sequence) -> reverse iterator over values of the sequence

|

| Return a reverse iterator

>>> a=['there','they','them','xyz']

>>> sorted(a) sorted 排序

['them', 'there', 'they', 'xyz']

>>>

>>> for i,t in enumerate(a): 枚举,包括索引和对应的元素值

... print i,t

...

0 there

1 they

2 them

3 xyz

>>> a=['peter','shawn','michael','andy']

>>> b=['peng','zhang','ye','mao']

>>> for i,j in zip(a,b):

... print ('%s %s' %(i,j)).title()

...

Peter Peng

Shawn Zhang

Michael Ye

Andy Mao

>>> a= ['this',1,'china','b','there']

>>> a.sort() sort()是一个内部方法,用于在列表中,它不会生成一个新的列表

>>> a

[1, 'b', 'china', 'there', 'this']

>>> a= ['this',1,'china','b','there']

>>> b=sorted(a) sorted()是python内建的函数,用来处理序列,两都不一样呵

>>> b

[1, 'b', 'china', 'there', 'this']

>>>

找出元素在列表中的位置,要先判断元素是否属于这个列表(in / not in),再用index()来找出具体位置

>>> b

[1, 'b', 'china', 'there', 'this']

>>> if 'china' in b:

... print b.index('china')

...

2

2:说明元素'china'在b列表的中第三个元素(编号从0开始)

1 #!/usr/bin/env python
2
3 stack = []
4
5 def pushit():
6 stack.append(raw_input('Enter new string: ').strip())
7
8 def popit():
9 if len(stack) == 0:
10 print 'Cannot pop from an empty stack!'
11 else:
12 print 'Removed [', 'stack.pop()', ']'
13
14 def viewstack():
15 print stack # calls str() internally
16
17 CMDs = {'u': pushit, 'o': popit, 'v': viewstack}
18
19 def showmenu():
20 pr = """
21 p(U)sh
22 p(O)p
23 (V)iew
24 (Q)uit
25
26 Enter choice: """
27
28 while True:
29 while True:
30 try:
31 choice = raw_input(pr).strip()[0].lower()
32 except (EOFError,KeyboardInterrupt,IndexError):
33 choice = 'q'
34
35 print '\nYou picked: [%s]' % choice
36 if choice not in 'uovq':
37 print 'Invalid option, try again'
38 else:
39 break  行29-39用于用户输入的合法性
40
41 if choice == 'q':
42 break
43 CMDs[choice]()
44
45 if __name__ == '__main__':
46 showmenu()


元组(tuple)

元组与列表类似,区别在于 1:元组是用小括号括起来的,但列表却是用方括号, 2: 元组是一种不可变的类型, 3: 当处理一组对象时,这个组默认是元组对象

元组的不可变并不是坏事,如将一个数据传给一个不了解的API的时候,可以保证我们的数据没有被修改

>>> a= 1,2,3,'test','peter',['abc','efg']

>>> type(a)

<type 'tuple'>
当处理一组对象时,这个组默认是元组对象

访问元组同访问列表一样,通过下标来实现

>>> a= 1,2,3,'test','peter',['abc','efg']

>>> a[3]

'test'

>>> a[5][0]

'abc'

>>>

因为元组是不可变类型,因此无法直接更新或者改变元组的数据,但可以通过现有的字符串片段重新构造一个元组

>>> a= 1,2,3,'test','peter',['abc','efg']

>>> a =a[0:3],'mytest','mypeter'

>>> a 这里的a已是新构造的元组了

((1, 2, 3), 'mytest', 'mypeter')

>>>

元组重复用*号,元组连接用+ 号

>>> a

((1, 2, 3), 'mytest', 'mypeter')

>>> a * 2

((1, 2, 3), 'mytest', 'mypeter', (1, 2, 3), 'mytest', 'mypeter')

>>> a

((1, 2, 3), 'mytest', 'mypeter')

>>> a + ('test1','peter1')

((1, 2, 3), 'mytest', 'mypeter', 'test1', 'peter1')

成员关系(in / not in)

>>> a

((1, 2, 3), 'mytest', 'mypeter')

>>> 'mytest' in a

True

>>> 'mypeter1' in a

False

>>>

列表与元组可以通过tuple(),list()函数进行互相变换

>>> a

((1, 2, 3), 'mytest', 'mypeter')

>>> list(a)

[(1, 2, 3), 'mytest', 'mypeter']

>>> tuple(list(a))

((1, 2, 3), 'mytest', 'mypeter')

>>>

元组是不可变的,但元组是一个容器,因此可以改变容器的对象

>>> a = (['test1','test2'],1,3,'abcdefg')

>>> a[0][1]='efghijk'

>>> a

(['test1', 'efghijk'], 1, 3, 'abcdefg')

>>>

所有的多对象的,用逗号分隔的,没有明确用符号定义,这些集合默认的类型都是元组

>>> 1,2,3,4,5

(1, 2, 3, 4, 5)

>>>

为了明确表示元组,建议用圆括号来表示元组

单元素元组


>>> ['abc']

['abc']

>>> type(['abc'])

<type 'list'>

>>> ('abc')

'abc'

>>> type(('abc'))

<type 'str'>



>>> type(('abc',)) 因为小括号也表示分组操作符,因此要表示单元素元组的一个好方法是在元组第一个 后面加一个逗号


<type 'tuple'>

>>>

一些与序弄相关的模块

>>> import array

>>> dir(array)

['ArrayType', '__doc__', '__file__', '__name__', 'array']

>>> help(array)

array是一个受限制的可变序列类型,要求所有的元素必须是相同的类型

operater包含函数调用形式的序列操作符,如operator.concat(m,n)等价于(m+n)

re: 正则表达式表达模块,

copy与deepcopy的区别

>>> person = ['name',['savings',10.00]]

>>> a=person[:]

>>> b=list(person)

>>> [id(x) for x in person,a,b]

[47091986859360, 47091986859936, 47091986859432]

>>>

>>> a[0]='peter'

>>> b[0]='peng'

>>> a,b

(['peter', ['savings', 10.0]], ['peng', ['savings', 10.0]])

>>> a[1][1]=50.00

>>> a,b

(['peter', ['savings', 50.0]], ['peng', ['savings', 50.0]])

浅拷贝=copy,浅拷贝只会在不可改变类型时创新的对象,如name为字符串,但[]代表的是列表,是可变序列,因此只会将它指向现一个引用里

· Python把一块数据存储在对象中,变量是对象的唯一引用;它们是计算机内存中特殊地点的名字。所有对象都具有唯一的身份号、类型和值。对象的类型不会改变,对于可变类型而言,它的值是可变的。id(obj)函数可用于检索对象的身份,也就是内存中的对象的地址。

· 每个对象都包含引用计数器,它记录当前有多少个变量正在引用该对象。当给对象指定一个变量或使对象成为列表或其它包容器的成员时,引用计数就增加;当从包容器中撤消、重新分配或删除对象时,引用计数减少。当引用计数达到0值时(即没有任何变量引用这个对象),python的回收机制会自动回收它使用的内存。注意,del可用来删除变量,但不能删除对象。


sys.getrefcount(obj)函数可返回给定对象的引用计数。
通过给列表分配一个变量能创建对列表的引用,如果要创建列表的副本就要理解浅副本和深副本的概念。

· 列表或其他包容器对象的浅副本(Shallow)能够生成对象本身的副本,但也会创建对由列表包含的对象的引用。可用分片(object[:])和copy模块的copy(obj)函数创建。
· 列表或其他对象包容器对象的深副本能够生成对象本身的副本,并递归地生成所有子对象的副本。可用copy模块的deepcopy(obj)函数创建。

练习

>>> a=[ 5,32,3,67,90,89]

>>> a.sort()

>>> a

[3, 5, 32, 67, 89, 90] <======== 按大小排序

>>> b=[]

>>> for a in a:

... b.append(str(a))

...

>>> b.sort()

>>> b

['3', '32', '5', '67', '89', '90'] <========按字典顺序排序幕
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: