一、初试列表(与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
注:Python有一种特殊的语法通过将索引指定为-1,返回最后一个列表元素,-2返回倒数第二个,-3返回倒数第三个,以此类推;
eg:输入:
band=["the beatles","oasis","eagles"]
print(band[-1].title())
输出:
Eagles
二、修改,添加,删除元素
1、修改列表元素(定位序号后,直接改)
eg:输入:
band=["the beatles","oasis","eagles"]
print(band[1].title())
band[1]="blur"
print(band[1].title())
输出:
Oasis
Blur
2、在列表中添加元素(使用append(),它将把元素添加到末尾,还不知怎么添加到开头)
eg:输入:
band = ["the beatles", "oasis", "eagles"]
print(band)
band.append("blur")
print(band)
输出:
['the beatles', 'oasis', 'eagles']
['the beatles', 'oasis', 'eagles', 'blur']
注:实际中可以创建一个空表,实现动态添加元素
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']
3、从列表中删除元素
(1)若知道元素在列表中的位置,可以使用 del 语句删除元素,但无法再次使用它
eg:输入:
band = ["oasis", "the beatles", "U2"]
print(band)
del band[2]
print(band)
输出:
['oasis', 'the beatles', 'U2']
['oasis', 'the beatles']
(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']
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 = ["ww", "zzy", "zwj", "hxc"]
print(band.reverse())
输出:
None
7、确定列表的长度,用 len()
输入:
band = ["ww", "zzy", "zwj", "hxc"]
print(len(band))
输出:
4