课程概要:
1、列表的基本操作
2、列表常用函数
3、列表和字符串的比较
1、列表的基本操作
知识点:
◆ 列表(list)定义
◆ 索引和切片
◆ 基本操作
一、列表定义
◆ 对象类型:list
◆ 表示方法:[ ]
◆ 列表元素:“有容乃大”
Python列表中元素可以是任何Python 中的对象
例如:字符串 整数 浮点数
>>> a = ['laoshi', 25, 89.9]
>>> b = ['laoshi', 25, 89.9, ['laoshi', 25, 89.9]]
>>> a
['laoshi', 25, 89.9]
>>> b
['laoshi', 25, 89.9, ['laoshi', 25, 89.9]]
>>> c = ['Hello', 'Python']
>>> c
['Hello', 'Python']
二、索引和切片
◆ 索引:类似“字符串”
◆ 切片
◆ 反转
>>>a
['laoshi', 25, 89.9]
>>> b
['laoshi', 25, 89.9, ['laoshi', 25, 89.9]]
>>> c = ['Hello', 'Python']
>>> c
['Hello', 'Python']
>>> a
['laoshi', 25, 89.9]
>>> a[1]
25
>>> a[0]
'laoshi'
>>> a[2]
89.9
1、索引
索引原则:左包括,右不包括
>>> a[0 : 2]
['laoshi', 25]
>>> a[: 2]
['laoshi', 25]
>>> a[0: ]
['laoshi', 25, 89.9]
>>> a[-1] # -1 就是从右边数起
89.9
>>> b
['laoshi', 25, 89.9, ['laoshi', 25, 89.9]]
>>> b[3]
['laoshi', 25, 89.9]
>>> b[3][0] # 二维列表
'laoshi'
>>> a.index(25)
1
>>> a.index(89.9)
2
>>> a.index("Python") # 列表没有该元素,使用index则会报错
Traceback (most recent call last):
File
"<pyshell#27>", line 1, in <module>
a.index("Python")
ValueError: 'Python' is not in list
>>> a.index("laoshi")
0
2、切片和反转
>>> lst = [1, 2, 3, 4, 5, 6]
>>> lst[: : -1] # 列表的反转
[6, 5, 4, 3, 2, 1]
# sice([start],stop[, step])
# 起始值 终点值 步长
>>> lst[0 : 4] # 步长为1,为默认值,写不写都可以
[1, 2, 3, 4]
>>> lst[0 : 4 : 2]
[1, 3]
>>> lst[4 : 1 : -1]
[5, 4, 3]
>>> lst[: : -2]
[6, 4, 2]
>>> list(reversed(lst)) # 列表的反转
[6, 5, 4, 3, 2, 1]
三、基本操作
◆ 列表是一种序列
◆ len
◆ +
◆ *
◆ in
◆ max()
◆ cmp()
>>> lst
[1, 2, 3, 4, 5, 6]
>>> len(lst)
6
>>> lst + a
[1, 2, 3, 4, 5, 6, 'laoshi', 25, 89.9]
>>> a * 3
['laoshi', 25, 89.9, 'laoshi', 25, 89.9, 'laoshi', 25, 89.9]
>>> max(lst)
6
>>> min(lst)
1
>>> a
['laoshi', 25, 89.9]
>>> max(a)
'laoshi'
>>> min(a)
25
>>> cmp(a, lst) # 依次比较两个列表中的元素,相同则向后再比较
1
2、列表常用函数
知识点:
◆ 追加和扩展
◆ 其他函数
一、追加和扩展
◆ append
◆ extend
◆ 原地修改
1、append
>>>dir(list)
>>> help(list.append)
Help on method_descriptor:
append(...)
L.append(object) -- append object to end
>>> a = [1, 2]
>>> a.append(100)
>>> a
[1, 2, 100]
>>> a.append("Python")
>>> a
[1, 2, 100, 'Python']
>>> a.append(["google", "facebook"])
>>> a
[1, 2, 100, 'Python', ['google', 'facebook']]
>>> b = [1]
>>> id(b)
34938032
>>> b.append(5)
>>> id(b) # 原地修改
34938032
>>> b.append("laoqi")
>>> id(b)
34938032
>>> b
[1, 5, 'laoqi']
2、extend
>>> help(list.extend)
Help on method_descriptor:
extend(...)
L.extend(iterable) --
extend list by appending elements from the iterable
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]
>>> b
[4, 5, 6]
>>> a.extend("Python")
>>> a
[1, 2, 3, 4, 5, 6, 'P', 'y', 't', 'h', 'o', 'n']
>>> a.extend(5) # 整数是不可迭代的
Traceback (most recent call last):
File
"<pyshell#26>", line 1, in <module>
a.extend(5)
TypeError: 'int' object is not iterable
3、查看对象是否是可迭代的
>>> alst = [1, 2]
>>> hasattr(alst, '__iter__')
True
>>> hasattr("Python", '__iter__')
False
>>> hasattr(5, '__iter__')
False
>>> a = [1, 2]
>>> a.append([4, 5])
>>> a # 使用append, 则[4, 5] 是作为一个整体进行追加
[1, 2, [4, 5]]
>>> a.extend([4, 5])
>>> a # 使用extend, 则将[4, 5]的每个元素取出来进行追加
[1, 2, [4, 5], 4, 5]
二、 其他函数
◆ count()
◆ index()
◆ insert()
◆ pop() remove()
◆ reverse()
◆ sort()
1、count()
>>> help(list.count)
Help on method_descriptor:
count(...)
L.count(value) ->
integer -- return number of occurrences of value
>>> a = [1, 1, 1, 2, 2, 1]
>>> a.count(1)
4
>>> a.count(2)
2
2、index()
>>> help(list.index)
Help on method_descriptor:
index(...)
L.index(value, [start,
[stop]]) -> integer -- return first index of value.
Raises ValueError if the
value is not present.
>>> a
[1, 1, 1, 2, 2, 1]
>>> a.index(1) # 返回该值出现的第一次的索引值
0
>>> a.index(2)
3
3、insert()
>>> help(list.insert)
Help on method_descriptor:
insert(...)
L.insert(index, object) --
insert object before index
>>> a = ["Python", "web"]
>>> a.insert(1, "yummy") # 插入到当前列表索引为1的元素前
>>> a
['Python', 'yummy', 'web']
>>> a.insert(0, 23415435)
>>> a
[23415435, 'Python', 'yummy', 'web']
4、pop()
>>> help(list.pop)
Help on method_descriptor:
pop(...)
L.pop([index]) -> item
-- remove and return item at index (default last).
Raises IndexError if list
is empty or index is out of range.
>>> a
[23415435, 'Python', 'yummy', 'web']
>>> a.pop(1)
'Python'
>>> a
[23415435, 'yummy', 'web']
>>> a.pop()
'web'
>>> a
[23415435, 'yummy']
>>> a.pop(2)
Traceback (most recent call last):
File
"<pyshell#62>", line 1, in <module>
a.pop(2)
IndexError: pop index out of range
5、remove()
>>> help(list.remove)
Help on method_descriptor:
remove(...)
L.remove(value) -- removefirst occurrence of value.
Raises ValueError if the
value is not present.
>>> a = ["taishan", "huashan",
"huangshan"]
>>> a.remove("taishan") # 只删除第一个出现值
>>> a
['huashan', 'huangshan']
>>> a.remove("songshan")
Traceback (most recent call last):
File
"<pyshell#67>", line 1, in <module>
a.remove("songshan")
ValueError: list.remove(x): x not in list
6、reverse()
>>> help(list.reverse)
Help on method_descriptor:
reverse(...)
L.reverse() -- reverse *IN
PLACE*
>>> a = [1, 2, 3, 4] # 反转
>>> a[: : -1]
[4, 3, 2, 1]
>>> list(reversed(a))
[4, 3, 2, 1]
>>> a
[1, 2, 3, 4]
>>> a.reverse()
>>> a
[4, 3, 2, 1]
7、sort()
>>> help(list.sort)
Help on method_descriptor:
sort(...)
L.sort(cmp=None, key=None,
reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1
>>> help(sorted)
Help on built-in function sorted in module __builtin__:
sorted(...)
sorted(iterable, cmp=None,
key=None, reverse=False) --> new sorted list
>>> a = [5, 3, 9, 2]
>>> a.sort() # 升序
>>> a
[2, 3, 5, 9]
>>> b = [9, 2, 7, 4]
>>> b.sort(reverse=True) # 降序
>>> b
[9, 7, 4, 2]
Python排序相关文档: https://docs.python.org/2.7/howto/index.html
3、列表和字符串比较
知识点:
◆ 序列
◆ 列表和字符串的比较
一、序列
◆ 序列:数学上,序列是被排成一列的对象(或事件);这样,每个元素不是在其他元素之前,就是在其他元素之后。这里,元素之间的顺序非常重要。(来源于《维基百科》)
二、列表和字符串的比较
◆ 字符串是不可改变的
◆ 列表是可以原地修改的
◆ list() 和 str() 的使用
>>> a = [1, 2, 3]
>>> id(a)
34938032
>>> a.append(4)
>>> a
[1, 2, 3, 4]
>>> id(a)
34938032
>>> a[1] = 9 # 列表是可修改的
>>> a
[1, 9, 3, 4]
>>> b = "Python"
>>> b[1] = 'w' # 字符串不可修改的
Traceback (most recent call last):
File
"<pyshell#91>", line 1, in <module>
b[1] = 'w'
TypeError: 'str' object does not support item assignment
非要修改
>>> b[0] + 'w' + b[2:]
'Pwthon'
>>> b
'Python'
>>> a # 一维列表
[1, 9, 3, 4]
>>> m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # 多维列表
>>> m[1]
[4, 5, 6]
>>> m[1][1]
5
>>> m[1][0]
4
>>> b
'Python'
>>> list(b)
['P', 'y', 't', 'h', 'o', 'n']
>>> a = "Python is good"
>>> a.split(" ")
['Python', 'is', 'good']
>>> c = list(b)
>>> c
['P', 'y', 't', 'h', 'o', 'n']
>>> " ".join(c)
'P y t h o n'
>>> "".join(c)
'Python'