-
创建列表数据类型:由方括号([ ])定义,当然也可以用工厂方法list(iter)创建
-
访问列表的值:通过切片操作符([ ])和索引值或索引值范围访问
-
更新列表:可以在等号左边指定一个索引或者索引范围的方式来更新一个或几个元素,也可以用append()方法追加新元素到列表中
-
删除列表元素或列表本身:使用del L[index]的方法,或者使用remove(value)方法
1
2
3
4
5
6
7
|
>>> list1
=
[
'abc'
,
123
]
>>> list2
=
[
'xyz'
,
789
]
>>> list3
=
[
'abc'
,
123
]
>>> list1 < list2
True
>>> list2 < list3
False
|
-
切片操作:跟普通序列的方法一样,主要是列表中还可以创建类似于“多维数组”的数据结构,即list[1][1],只不过在这样使用之前,应该通过`L[1] = [ ]`这样的形式来在列表中创建一个空列表
-
成员关系操作(in, not in):判断元素是否在列表中
-
连接操作符(+):把两个或多个列表连接起来,不过与字符串的连接操作一样,同样会创建每一个列表对象,所以可以使用list.extend()方法来代替
1
2
3
4
|
>>> aList
=
[
'xpleaf'
]
>>> aList
+
=
'clyyh'
>>> aList
[
'xpleaf'
,
'c'
,
'l'
,
'y'
,
'y'
,
'h'
]
|
-
重复操作符(*):重复生成列表中的元素
1
2
3
4
|
>>> [ 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
]
|
-
对两个列表的元素进行比较
-
如果比较的元素是同类型的,则比较其值,返回结果
-
如果两个元素不是同一种类型,则检查它们是否是数字
a.如果是数字,执行必要的数字强制类型转换,然后比较
b.如果有一方的元素是数字,则另一方的元素“大”(数字是“最小的”)
c.否则,通过类型名字的字母顺序进行比较
-
如果有一个列表首先到达末尾,则另一个长一点的列表“大”
-
如果用尽了两个列表的元素而且所有元素都是相等的,则返回0
-
len():返回列表中元素的个数
1
2
3
|
>>> L
=
[
123
,
456
]
>>>
len
(L)
2
|
-
max()和min():如果列表元素是同类型的,就会非常有用,当然,列表元素中混合对象的结构越复杂,则返回的结构准确性就越差
1
2
3
4
|
>>>
min
(L)
123
>>>
max
(L)
456
|
-
sorted()和reversed():排序和翻转
1
2
3
4
5
6
7
8
|
>>>
list
[
'abc'
,
123
,
'efg'
,
'456'
]
>>>
for
value
in
reversed
(
list
):
...
print
value,
...
456
efg
123
abc
>>>
sorted
(L)
[
123
,
'456'
,
'abc'
,
'efg'
]
|
-
enumerate()和zip()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
>>> name
=
[
'xpleaf'
,
'clyyh'
,
'yonghaoye'
]
>>>
enumerate
(name)
<
enumerate
object
at
0x7f5f5f41b5f0
>
>>>
list
(
enumerate
(name))
[(
0
,
'xpleaf'
), (
1
,
'clyyh'
), (
2
,
'yonghaoye'
)]
>>>
for
value
in
enumerate
(name):
...
print
value,
...
(
0
,
'xpleaf'
) (
1
,
'clyyh'
) (
2
,
'yonghaoye'
)
>>>
for
index, value
in
enumerate
(name):
...
print
index, value
...
0
xpleaf
1
clyyh
2
yonghaoye
>>>
>>> fn
=
[
'ian'
,
'stuart'
,
'david'
]
>>> ln
=
[
'bairnson'
,
'elliott'
,
'paton'
]
>>>
zip
(fn, ln)
[(
'ian'
,
'bairnson'
), (
'stuart'
,
'elliott'
), (
'david'
,
'paton'
)]
|
-
sum()
1
2
3
4
5
|
>>> a
=
[
6
,
4
,
5
]
>>>
sum
(a)
15
>>>
sum
(a,
5
)
20
|
-
list()和tuple():接受可迭代对象,并通过浅拷贝数据来创建一个新的列表或元组
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>> aList
=
[
'tao'
,
93
,
99
,
'time'
]
>>> aTuple
=
tuple
(aList)
>>> aList, aTuple
([
'tao'
,
93
,
99
,
'time'
], (
'tao'
,
93
,
99
,
'time'
))
>>> anotherList
=
list
(aTuple)
>>> aList
=
=
anotherList
True
>>> aList
is
anotherList
False
>>> [
id
(x)
for
x
in
aList, aTuple, anotherList ]
[
140047597735448
,
140047596625512
,
140047596743208
]
>>> [
id
(x)
for
x
in
aList[
0
], aTuple[
0
], anotherList[
0
] ]
[
140047628190584
,
140047628190584
,
140047628190584
]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
class
list
(
object
)
|
list
()
-
> new empty
list
|
list
(iterable)
-
> new
list
initialized
from
iterable's items
|
| Methods defined here:
|
| __add__(...)
| x.__add__(y) <
=
=
> x
+
y
|
| __contains__(...)
| x.__contains__(y) <
=
=
> y
in
x
|
| __delitem__(...)
| x.__delitem__(y) <
=
=
>
del
x[y]
|
| __delslice__(...)
| x.__delslice__(i, j) <
=
=
>
del
x[i:j]
|
| Use of negative indices
is
not
supported.
……
|
1
2
|
>>>
dir
(
list
)
#或dir([])
[
'__add__'
,
'__class__'
,
'__contains__'
,
'__delattr__'
,
'__delitem__'
,
'__delslice__'
,
'__doc__'
,
'__eq__'
,
'__format__'
,
'__ge__'
,
'__getattribute__'
,
'__getitem__'
,
'__getslice__'
,
'__gt__'
,
'__hash__'
,
'__iadd__'
,
'__imul__'
,
'__init__'
,
'__iter__'
,
'__le__'
,
'__len__'
,
'__lt__'
,
'__mul__'
,
'__ne__'
,
'__new__'
,
'__reduce__'
,
'__reduce_ex__'
,
'__repr__'
,
'__reversed__'
,
'__rmul__'
,
'__setattr__'
,
'__setitem__'
,
'__setslice__'
,
'__sizeof__'
,
'__str__'
,
'__subclasshook__'
,
'append'
,
'count'
,
'extend'
,
'index'
,
'insert'
,
'pop'
,
'remove'
,
'reverse'
,
'sort'
]
|
列表类型内建函数(方法) | |
列表函数(方法) | 作用 |
list.append(obj) | 向列表中添加一个对象obj |
list.count(obj) | 返回一个对象obj在列表中出现的次数 |
list.extend(seq) | 把序列seq的内容添加到列表中 |
list.index(obj, i=0, j=len(list)) | 返回list[k]==obj的k值,并且k的范围在i<=k<j;否则引发ValueError异常 |
list.insert(index, obj) | 在索引量为index的位置插入对象obj |
list.pop(index=-1) | 删除并返回指定位置的对象,默认是最后一个对象 |
list.remove(obj) | 从列表中删除对象obj |
list.revese() | 原地翻转列表 |
list.sort(func=None, key=None, reverse=False) | 以指定的方式排序列表中的成员,如果func和key参数指定,则按照指定的方式比较各个元素,如果reverse标志被置为True,则列表以反序排列 |
-
insert()和append()
1
2
3
4
5
6
7
8
9
10
11
12
|
>>> music_media
=
[
45
]
>>> music_media
[
45
]
>>> music_media.insert(
0
,
'compact disc'
)
>>> music_media
[
'compact disc'
,
45
]
>>> music_media.append(
'long playing record'
)
>>> music_media
[
'compact disc'
,
45
,
'long playing record'
]
>>> music_media.insert(
2
,
'8-track tape'
)
>>> music_media
[
'compact disc'
,
45
,
'8-track tape'
,
'long playing record'
]
|
-
index()
1
2
3
4
5
6
7
8
9
10
11
|
>>> music_media
[
'compact disc'
,
45
,
'8-track tape'
,
'long playing record'
]
>>> music_media.index(
45
)
1
>>> music_media.index(
'8-track tape'
)
2
>>>
>>> music_media.index(
'xpleaf'
)
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
ValueError:
'xpleaf'
is
not
in
list
|
1
2
3
4
5
6
7
8
9
|
>>>
45
in
music_media
True
>>>
'xpleaf'
in
music_media
False
>>>
for
eachMediaType
in
(
45
,
'xpleaf'
):
...
if
eachMediaType
in
music_media:
...
print
music_media.index(eachMediaType)
...
1
|
-
sort()和reverse()
1
2
3
4
5
6
7
8
9
|
>>> music_media
[
'compact disc'
,
45
,
'8-track tape'
,
'long playing record'
]
>>> music_media.sort()
>>> music_media
[
45
,
'8-track tape'
,
'compact disc'
,
'long playing record'
]
>>>
>>> music_media.reverse()
>>> music_media
[
'long playing record'
,
'compact disc'
,
'8-track tape'
,
45
]
|
|
-
extend()
1
2
3
4
5
6
|
>>> music_media
[
'long playing record'
,
'compact disc'
,
'8-track tape'
,
45
]
>>> new_media
=
[
'24/96 digital audio disc'
,
'DVD Audio disc'
,
'Super Audio CD'
]
>>> music_media.extend(new_media)
>>> music_media
[
'long playing record'
,
'compact disc'
,
'8-track tape'
,
45
,
'24/96 digital audio disc'
,
'DVD Audio disc'
,
'Super Audio CD'
]
|
1
2
3
4
5
6
7
8
9
10
|
xpleaf@leaf:~$ echo
'Welcome to GDUT'
> welcome.txt
xpleaf@leaf:~$ python
……
>>> welcome
=
[]
>>> welcome.append(
'Welcome Message'
)
>>> f
=
open
(
'welcome.txt'
,
'r'
)
>>> welcome.extend(f)
>>> f.close()
>>> welcome
[
'Welcome Message'
,
'Welcome to GDUT\n'
]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#!/usr/bin/env python
stack
=
[]
def
pushit():
stack.append(
raw_input
(
'Enter New string: '
).strip())
def
popit():
if
len
(stack)
=
=
0
:
print
'Cannot pop from an empty stack!'
else
:
print
'Removed ['
, `stack.pop()`,
']'
def
viewstack():
print
stack
CMDs
=
{
'u'
: pushit,
'o'
: popit,
'v'
: viewstack}
def
showmenu():
pr
=
'''
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice: '''
while
True
:
while
True
:
try
:
choice
=
raw_input
(pr).strip()[
0
].lower()
except
(EOFError, KeyboardInterrupt, IndexError):
choice
=
'q'
print
'\nYou picked: [%s]'
%
choice
if
choice
not
in
'uovq'
:
print
'Invalid option, try again'
else
:
break
if
choice
=
=
'q'
:
break
CMDs[choice]()
if
__name__
=
=
'__main__'
:
showmenu()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
xpleaf@leaf:~$ python stack.py
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice: u
You picked: [u]
Enter New string: clyyh
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice: u
You picked: [u]
Enter New string: clyyh
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice:
v
You picked: [
v
]
[
'clyyh'
,
'clyyh'
]
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice: o
You picked: [o]
Removed [
'clyyh'
]
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice: q
You picked: [q]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#!/usr/bin/env python
queue
=
[]
def
enQ():
queue.append(
raw_input
(
'Enter New string: '
).strip())
def
deQ():
if
len
(queue)
=
=
0
:
print
'Cannot pop from an empty stack!'
else
:
print
'Removed ['
, `queue.pop(
0
)`,
']'
def
viewQ():
print
queue
CMDs
=
{
'e'
: enQ,
'd'
: deQ,
'v'
: viewQ}
def
showmenu():
pr
=
'''
(E)nqueue
(D)equeue
(V)iew
(Q)uit
Enter choice: '''
while
True
:
while
True
:
try
:
choice
=
raw_input
(pr).strip()[
0
].lower()
except
(EOFError, KeyboardInterrupt, IndexError):
choice
=
'q'
print
'\nYou picked: [%s]'
%
choice
if
choice
not
in
'devq'
:
print
'Invalid option, try again'
else
:
break
if
choice
=
=
'q'
:
break
CMDs[choice]()
if
__name__
=
=
'__main__'
:
showmenu()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
xpleaf@leaf:~
/PycharmProjects/Python_book/6
$ python queue.py
(E)nqueue
(D)equeue
(V)iew
(Q)uit
Enter choice: e
You picked: [e]
Enter New string: xpleaf
(E)nqueue
(D)equeue
(V)iew
(Q)uit
Enter choice: e
You picked: [e]
Enter New string: clyyh
(E)nqueue
(D)equeue
(V)iew
(Q)uit
Enter choice:
v
You picked: [
v
]
[
'xpleaf'
,
'clyyh'
]
(E)nqueue
(D)equeue
(V)iew
(Q)uit
Enter choice: d
You picked: [d]
Removed [
'xpleaf'
]
(E)nqueue
(D)equeue
(V)iew
(Q)uit
Enter choice:
v
You picked: [
v
]
[
'clyyh'
]
(E)nqueue
(D)equeue
(V)iew
(Q)uit
Enter choice: q
You picked: [q]
|
-
定义:元组使用圆括号定义
-
功能:元组是不可变类型的数据结构
-
介绍可用于大部分对象的操作符和内建函数
-
介绍针对序列类型的内建函数
-
介绍针对特定序列类型的方法
-
创建元组并赋值
1
2
3
4
5
6
7
8
|
>>> aTuple
=
(
'xpleaf'
,)
>>>
type
(aTuple)
<
type
'tuple'
>
>>> nTuple
=
(
'xpleaf'
)
>>>
type
(nTuple)
<
type
'str'
>
>>>
tuple
(
'xpleaf'
)
(
'x'
,
'p'
,
'l'
,
'e'
,
'a'
,
'f'
)
|
-
访问元组中的值:跟列表一样,通过切片操作符([ ])和索引值或索引值范围访问
-
更新元组:因为元组是不可变类型,所以更新元组实际上是重新生成一个新的元组对象,这点和字符串是一样的
-
删除元组元素以及元组本身:同样的,不能只删除元组中的某一个元素,因为它是不可变对象,当然要删除一个元组,只需要使用del即可,只是一般情况下并不需要这样做
-
创建、重复、连接操作
1
2
3
4
5
6
7
8
9
|
>>> aList
=
[
'xpleaf'
]
>>> aList
+
=
'clyyh'
>>> aList
[
'xpleaf'
,
'c'
,
'l'
,
'y'
,
'y'
,
'h'
]
>>> aTuple
=
(
'xpleaf'
,)
>>> aTuple
+
=
'clyyh'
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
TypeError: can only concatenate
tuple
(
not
"str"
) to
tuple
|
-
成员关系操作、切片操作
-
内建函数:主要是str()、len()、max()、min()、cmp()、list()
-
操作符:主要是<、>和==
1
2
|
>>> (
3
,
4
) < (
5
,
6
)
#即可以这样来比较数的大小
True
|
-
连接操作:可以使用连接操作,这跟字符串是一样的,但本质上也是没有改变元组
1
2
3
4
5
6
7
8
|
>>> t
=
(
'xpleaf'
,)
>>>
id
(t)
140420395249936
>>> t
+
=
(
'clyyh'
,)
>>> t
(
'xpleaf'
,
'clyyh'
)
>>>
id
(t)
140420395089432
|
-
修改元组的可变元素对象:这看上去就像可以修改元组的元素
1
2
3
4
5
6
7
8
9
10
|
>>> aTuple
=
([
123
,
'xpleaf'
],
'clyyh'
)
>>> aTuple
([
123
,
'xpleaf'
],
'clyyh'
)
>>>
id
(aTuple)
140420395089576
>>> aTuple[
0
][
1
]
=
'yonghaoye'
>>> aTuple
([
123
,
'yonghaoye'
],
'clyyh'
)
>>>
id
(aTuple)
140420395089576
|
-
所有的多对象的、逗号分隔的、没有明确用符号定义的(比如用方括号表示列表和用圆括号表示元组),这些集合默认的类型都是元组
1
2
3
4
5
6
7
|
>>>
'xpleaf'
,
(
'xpleaf'
,)
>>>
'xpleaf'
,
'clyyh'
(
'xpleaf'
,
'clyyh'
)
>>> x, y
=
1
,
2
>>> x, y
(
1
,
2
)
|
-
所有函数返回的多对象(不包括有符号封装的)都是元组类型,注意,有符号封装的多对象集合其实是返回的一个单一的容器对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
>>>
def
reMutileObj():
...
return
'xpleaf'
,
'clyyh'
...
>>>
def
reOneObj():
...
return
[
'xpleaf'
,
'clyyh'
]
...
>>>
def
reMutileObj2():
...
return
(
'xpleaf'
,
'clyyh'
)
...
>>> reMutileObj()
(
'xpleaf'
,
'clyyh'
)
>>> reOneObj()
[
'xpleaf'
,
'clyyh'
]
>>> reMutileObj2()
(
'xpleaf'
,
'clyyh'
)
|
1
2
|
>>>
4
,
2
<
3
,
5
(
4
,
True
,
5
)
|
1
2
3
4
|
>>> (
'xpleaf'
,)
(
'xpleaf'
,)
>>>
'xpleaf'
,
(
'xpleaf'
,)
|
-
array:一种受限制的可变序列类型,要求所有的元素必须都是相同的类型
-
copy:提供浅拷贝和深拷贝的能力
-
operator:包含函数调用形式的序列操作符,比如operator.concat(m, n)就相当于连接操作(m+n)
-
re:Perl风格的正则表达式(和匹配)
-
浅拷贝:对一个对象进行浅拷贝其实是新创建了一个类型跟原对象一样,其内容还是原来对象元素的引用,换句话说,这个拷贝的对象本身是新的,但是它的内容不是
-
完全切片操作:下面操作会有
-
利用工厂函数:比如list()、dict()等
-
使用copy模块的copy()函数
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>> person
=
[
'name'
, (
'hobby'
, [
1
,
2
])]
>>> xpleaf
=
person[:]
>>>
from
copy
import
deepcopy as dcp
>>> cl
=
dcp(person)
>>>
>>> xpleaf[
0
]
=
'xpleaf'
>>> cl[
0
]
=
'cl'
>>> person, xpleaf, cl
([
'name'
, (
'hobby'
, [
1
,
2
])], [
'xpleaf'
, (
'hobby'
, [
1
,
2
])], [
'cl'
, (
'hobby'
, [
1
,
2
])])
>>>
>>> xpleaf[
1
][
1
][
0
]
=
'clyyh'
>>> person, xpleaf, cl
([
'name'
, (
'hobby'
, [
'clyyh'
,
2
])], [
'xpleaf'
, (
'hobby'
, [
'clyyh'
,
2
])], [
'cl'
, (
'hobby'
, [
1
,
2
])])
|