序列(sequence): 字符串(basestring, str+unicode),列表,元组。 序列本身包含迭代的概念
N=len(sequence)
- 基本相关操作和优先级
1) index
2) 切片
0<=index<len(seq)
-len(seq)<=index<=-1
>>> seq[:]
'123'
>>> seq[:2]
'12'
>>> seq[1:2]
'2'
>>> seq[::-1]
'321'
>>> seq1="123123"
>>> seq1[3::-1]
'1321'
>>> seq1[3:1:-1]
'13'
切片索引可以使用大索引:
>>> seq1[-100:100]
'123123'
>>> for i in range(-1,-len(seq1),-1):
... print seq1[:i]
...
12312
1231
123
12
1
但是有没有发现一个问题:不能输出123123,而且起始位置又不能把-1替换成0,那该怎么办呢?
>>> for i in None+range(-1,-len(seq1),-1):
... print seq1[:i]
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'NoneType' and 'list'
>>> for i in [None]+range(-1,-len(seq1),-1):
... print seq1[:i]
...
123123
12312
1231
123
12
1
3) +和*(连接和重复)>>> seq="123"
>>> seq*3
'123123123'
>>> seq+seq
'123123'
>>>
对字符串来说,“+”操作不是最快和最有效的,不如把所有的子字符串放在一个列表或可迭代对象中,然后调用一个join或者extend方法可以节约内存(依赖于可变对象)。还可以用%s%s来输出连字符串
4) extend, join
[1,2].extend(range(5))是没有返回值的
>>> ls=[1,2]
>>> ls.extend(range(5))
>>> ls
[1, 2, 0, 1, 2, 3, 4]
5) in, not in(复杂度O(n))
6) enumerate()
7) len()
8) max(item,key=) or max(arg1, arg2,...,key=), 同min
key=函数名,lambda,None为变量添置条件
9) reversed()
10) sorted(iter, cmp=,key=,reversed=True/False)
cmp和key都接受一个函数,不同的是,key指定的函数,只能接受一个元素
11) sum(seq, init)
12) zip()
zip([])返回一个列表
>>> zip(l)
[(1,), (2,), (3,)]
>>> l1=[4,5,6]
>>> zip(l,l1)
[(1, 4), (2, 5), (3, 6)]
>>> l2=[7,8,9]
>>> zip(l,l1,l2)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
将对象作为参数,并将内容浅(深)拷贝到新生成的对象中,list(), str(), tuple(), unicode(), basestring()等
>>> s="abc"
>>> list(s)
['a', 'b', 'c']
>>> tuple(s)
('a', 'b', 'c')
>>> a="hello world"
>>> a[:3]+"z"+a[4:]
'helzo world'
>>> a="hello world"
>>> a[:3]+"z"+a[4:]
'helzo world'
>>> del a[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object doesn't support item deletion
>>> del a
>>> s=(1,2,3)
>>> del s[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object doesn't support item deletion
>>> l=[1,2,3]
>>> del l[1]
>>> l
[1, 3]
>>> import string
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'
>>> print "hello"'world'
helloworld
>>> u"张\n"
u'\u5f20\n'
>>> ur"张\n"
u'\u5f20\\n'
使用一个或多个字节来表示字符,打破ASCII码的限制,字符串+unicode会转换成unicode
工厂函数:unicode()和unichar()
codec定义文本跟二进制的转换方式
例题:
Unicode字符串,UTF-8编码,写入文件,解码回Unicode字符串对象
<span style="font-family:Arial;font-size:12px;">#!/usr/bin/env python
codec="utf-8"
file="unicode.txt"
s=u"hello world\n"
b=s.encode(codec)
f=open(file,"w")
f.write(b)
f.close()
f=open(file,"r")
b=f.read()
f.close()
s=b.decode(codec)
print s
</span>string模块不适用于非ASCII字符,只有写入文件,数据库或者网络时,才调用encode(),读取时才用decode()(python中还有pickle模块只兼容ASCII字符串),注意unicode的支持,兼容其他的语言本身是一个工程
2. 列表
- 列表是不可变的,列表可以包含用户自定义元素
>>> l
[1, [1, 2, 3], 3]
>>> if 2 in l:
... print "Yes"
... else:
... print "No"
...
No
- +两边必须是相同类型的,举例>>> l=[1,2,3]
>>> l+4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "int") to list
所以可以采用append()
- cmp()比较的是字典序,而不是ASCII字母序
- sort()默认采用归并排序,复杂度是O(lgn)
3. 元组
- 元组是不可变的,我们常常可以利用list(tuple)来使对象变成可变的,如果我们有某个列表,但是不想让别人修改里面的数据,此时,可以采用tuple(list)
>>> l
(1, 2, [3, 4])
>>> l[0]=1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> l=l[0]+(5,)+l[1:]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
>>> l=l[:1]+(5,)+l[1:]
>>> l
(1, 5, 2, [3, 4])
>>> l[3][1]=6
>>> l
(1, 5, 2, [3, 6])
>>> l
(1, 2, 3)
>>> s=list(l)
>>> s[0]=0
>>> tuple(s)
(0, 2, 3)
>>> l=(1,2,3)
>>> l[0]=0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> id(l)
4399197392
>>> id(list(l))
4399250176
>>> id(tuple(list(l)))
4399197072
浅拷贝:切片,工厂函数,使用copy函数
>>> person=["name",["savings",100]]
>>> jon=person
>>> jon[0]="jon"
>>> person
['jon', ['savings', 100]]
>>> person=["name",["savings",100]]
>>> jon=person[:]
>>> jon[0]="jon"
>>> person
['name', ['savings', 100]]
>>> jon
['jon', ['savings', 100]]
>>> jon[1][1]=50
>>> person
['name', ['savings', 50]]
>>> jon
['jon', ['savings', 50]]
低层浅拷贝:指向相同对象,一变全变
高层浅拷贝:对于原子型对象,起先指向同一个对象,然后修改对象时会创建新对象,对于可变对象,一变全变
[id(x) for x in ?, ?, ?]
[id(x) for x in ?]
深拷贝:
>>> person=["name",["savings",100]]
>>> jon=copy.deepcopy(person)
>>> jon[1][1]=50
>>> jon
['name', ['savings', 50]]
>>> person
['name', ['savings', 100]]
1697

被折叠的 条评论
为什么被折叠?



