一、初试列表(与C中的数组类似)
1、列表的定义和输出(约定俗成,用 [ ] 引起列表)
eg:输入:
band=["the beatles","oasis","eagles"]
print(band)
输出:
['the beatles', 'oasis', 'eagles']
2、列表元素的访问(0表示第一个元素)
eg:输入:
band=["the beatles","oasis","eagles"]
print(band[1].title())
输出:
Oasis
注:
(1)Python有一种特殊的语法通过将索引指定为-1,返回最后一个列表元素,-2返回倒数第二个,-3返回倒数第三个,以此类推;
eg:输入:
band=["the beatles","oasis","eagles"]
print(band[-1].title())
输出:
Eagles
(2)用 .index 获取元素索引(若列表中元素有重复,只返回序列靠前的那一个)
输入:
band=["the beatles", "oasis", "eagles"]
print(band.index("oasis"))
输出:
1
还可以指定位置区域:
输入:
band=["the beatles", "oasis", "eagles", "blur", "the kinks", "oasis", 'the who']
print(band.index("oasis", 1, 6))
print(band.index("oasis", 2, 6))
输出:
1
5
二、修改,添加,删除元素
1、修改列表元素(定位序号后,直接改)
eg:
(1)输入:
band=["the beatles","oasis","eagles"]
print(band[1].title())
band[1]="blur"
print(band[1].title())
输出:
Oasis
Blur
(2)也可以用切片替换
输入:
band = ["eagles", "blur", "the kinks", 'the who']
band[1:3] = ["the beatles", "oasis", "U2"]
print(band)
输出:
['eagles', 'the beatles', 'oasis', 'U2', 'the who']
2、在列表中添加元素
eg:
(1)使用append(),把一个元素添加到末尾
输入:
band = ["the beatles", "oasis", "eagles"]
print(band)
band.append("blur")
print(band)
输出:
['the beatles', 'oasis', 'eagles']
['the beatles', 'oasis', 'eagles', 'blur']
(2)添加多个元素,使用 .extend()
输入:
band = ["the beatles", "oasis"]
band1 = ["eagles", "blur", "the kinks", 'the who']
band.extend(band1)
print(band)
输出:
['the beatles', 'oasis', 'eagles', 'blur', 'the kinks', 'the who']
(3)使用切片来替换
输入:
band = ["the beatles", "oasis"]
band1 = ["eagles", "blur", "the kinks", 'the who']
band[1::] = band1
print(band)
输出:
['the beatles', 'eagles', 'blur', 'the kinks', 'the who']
注:
(1)实际中可以创建一个空表,实现动态添加元素
eg:输入:
band = []
band.append("oasis")
band.append("the beatles ")
band.append("blur")
print(band)
输出:
['oasis', 'the beatles ', 'blur']
注:band = [ ] 这样创建的空表会有警告
This list creation could be rewritten as a list literal
此时需要用到函数 list(),意为将列表格式化
band = list([])
band.append("oasis")
band.append("the beatles ")
band.append("blur")
print(band)
空表也可以直接
band = list()
band.append("oasis")
band.append("the beatles ")
band.append("blur")
print(band)
特别的,也可以格式化字符串变量:
输出:
string = "hello world"
L = list(string)
print(L)
输出:
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
(2)用 range() 创建列表
输入:
liSt = [i for i in range(1, 11)]
print(liSt)
输出:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
3、从列表中删除元素
(1)若知道元素在列表中的位置,可以使用 del 语句删除元素,但无法再次使用它
eg:输入:
band = ["oasis", "the beatles", "U2"]
print(band)
del band[2]
print(band)
输出:
['oasis', 'the beatles', 'U2']
['oasis', 'the beatles']
注:也可删除整个列表
band = ["eagles", "blur", "the kinks", 'the who']
del band
print(band)
(2) .pop(位置) 可以删除任意位置的元素,但是可以继续使用它
eg:
a、若 () 里为空,默认删除最后一个
输入:
band = ["oasis", "the beatles", "U2"]
print(band)
popped_bands = band.pop()
print(band)
print(popped_bands)
输出:
['oasis', 'the beatles', 'U2']
['oasis', 'the beatles']
U2
b、 输入:
band = ["oasis", "the beatles", "U2"]
print(band)
popped_bands = band.pop(1)
print(band)
print(popped_bands)
输出:
['oasis', 'the beatles', 'U2']
['oasis', 'U2']
the beatles
(3)根据值删除元素,使用 .remove(元素)
注:.remove() 只能删除顺序遍历后的第一个,若要全部删除,需用到循环
eg:输入:
band = ["oasis", "the beatles", "U2"]
print(band)
band.remove("oasis")
print(band)
输出:
['oasis', 'the beatles', 'U2']
['the beatles', 'U2']
(4)用切片删除
输入:
band = ["eagles", "blur", "the kinks", 'the who']
band[1:3] = []
print(band)
输出:
['eagles', 'the who']
(5)用 .clear() 清空
输入:
band = ["eagles", "blur", "the kinks", 'the who']
band.clear()
print(band)
输出:
[]
5、在列表中插入元素(使用 insert(位置,元素) 可以在列表的任何位置插入新元素)
输入:
band = ["oasis", "the beatles", "U2"]
band.insert(0, "eagles")
print(band)
输出:
['eagles', 'oasis', 'the beatles', 'U2']
6、对列表进行排序
(1)使用 .sort() ,默认升序排列,若要降序,需传递参数 reverse=True(这个排序是永久的,不可逆),对原列表进行操作
eg:
a. 对数字
输入:
band = [11, 2, 1, 3]
print(band)
band.sort()
print(band)
band.sort(reverse=True)
print(band)
输出:
[11, 2, 1, 3]
[1, 2, 3, 11]
[11, 3, 2, 1]
b. 对字符
输入:
band = ["ww", "zzy", "zwj", "hxc"]
print(band)
band.sort()
print(band)
band.sort(reverse=True)
print(band)
输出:
['ww', 'zzy', 'zwj', 'hxc']
['hxc', 'ww', 'zwj', 'zzy']
['zzy', 'zwj', 'ww', 'hxc']
(2)使用 sorted() 对列表进行排序,会生成一个新的列表,原列表不变。默认升序排列,若要降序,需传递参数 reverse=True
输入:
band = ["ww", "zzy", "zwj", "hxc"]
print(band)
print(sorted(band))
print(band)
print(sorted(band, reverse=True))
输出:
['ww', 'zzy', 'zwj', 'hxc']
['hxc', 'ww', 'zwj', 'zzy']
['ww', 'zzy', 'zwj', 'hxc']
['zzy', 'zwj', 'ww', 'hxc']
(3)反转列表:用 .reverse()
输入:
band = ["ww", "zzy", "zwj", "hxc"]
print(band)
band.reverse()
print(band)
输出:
['ww', 'zzy', 'zwj', 'hxc']
['hxc', 'zwj', 'zzy', 'ww']
也可以用切片操作来反转列表:
输入:
band = ["the beatles", "oasis", "eagles", "blur", "the kinks", 'the who']
print(band[::-1])
输出:
['the who', 'the kinks', 'blur', 'eagles', 'oasis', 'the beatles']
注意:这样操作是不对的
输入:
band = ["ww", "zzy", "zwj", "hxc"]
print(band.reverse())
输出:
None
7、确定列表的长度,用 len()
输入:
band = ["ww", "zzy", "zwj", "hxc"]
print(len(band))
输出:
4