?列表
使用? []? 或? list()? 创建。
>>> empty_list = []
>>> empty_list = [ ]
>>> weekdays = ["Monday","Tuesday","Wednesday","Tuesday", "Friday"]
>>> empty__list = list()
>>> empty__list
[]
>>> empty_list
[]
>>> weekdays
['Monday', 'Tuesday', 'Wednesday', 'Tuesday', 'Friday']
list()? 可以将其他数据类型转换成列表类型
>>> list("cat")
['c', 'a', 't']
>>> a_tuple = ("hhh","jjj",'00')
>>> list(a_tuple)
['hhh', 'jjj', '00']
split()根据分隔符分割字符串成列表
>>> date = "1991.1.1"
>>> date.split(".")
['1991', '1', '1']
>>> date2 = "1991..1..1"
>>> date2.split(".")
['1991', '', '1', '', '1']
>>> date2.split("..")
['1991', '1', '1']
可以通过偏移量获取对应位置元素
>>> weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
>>> weekdays[3]
'Thursday'
>>> weekdays[-1]
'Friday'
列表可以包含列表、字符串、数字;
>>> list1 = ["this",'is',"list",1]
>>> list2 = [1,2,3]
>>> list = [list1,"list",list2]
>>> list
[['this', 'is', 'list', 1], 'list', [1, 2, 3]]
>>> list[1]
'list'
>>> list[0]
['this', 'is', 'list', 1]
>>> list[0][0]
'this'
>>> list[-1][0]
1
通过偏移量修改列表内元素
>>> list = [1,2,3,4,"hhhh"]
>>> list[1] = "aaa"
>>> list
[1, 'aaa', 3, 4, 'hhhh']
可切片提取元素
>>> list = [1,2,3,4,"hhhh"]
>>> list[1] = "aaa"
>>> list
[1, 'aaa', 3, 4, 'hhhh']
>>> list[1:]
['aaa', 3, 4, 'hhhh']
>>> list[:3]
[1, 'aaa', 3]
>>> list[1:3]
['aaa', 3]
>>> list[::3]
[1, 4]
>>> list[::-1]
['hhhh', 4, 3, 'aaa', 1]
append() 添加元素至列表尾部
>>> list
[1, 'aaa', 3, 4, 'hhhh']
>>> list.append(9)
>>> list
[1, 'aaa', 3, 4, 'hhhh', 9]
>>> list1 = [1,2,3]
>>> list2 = ["a","b","c"]
>>> list1.append(list2)
>>> list1
[1, 2, 3, ['a', 'b', 'c']]
extend() 、+= 合并列表
>>> list1 = [1,2,3]
>>> list2 = []
>>> list1 = [1,2,3]
>>> list2 = ["a","b","c"]
>>> list1.extend(list2)
>>> list1
[1, 2, 3, 'a', 'b', 'c']
>>> list1 += list2
>>> list1
[1, 2, 3, 'a', 'b', 'c', 'a', 'b', 'c']
>>> list1 += list1
>>> list1
[1, 2, 3, 'a', 'b', 'c', 'a', 'b', 'c', 1, 2, 3, 'a', 'b', 'c', 'a', 'b', 'c']
insert() 在指定位置插入元素
>>> list1.insert(0,list2)
>>> list1
[['a', 'b', 'c'], 1, 2, 3]
>>> list2.insert(1,"insert1")
>>> list2
['a', 'insert1', 'b', 'c']
del 删除指定位置的元素(是python语句,不是方法)
>>> list2
['a', 'insert1', 'b', 'c']
>>> del list2[0]
>>> list2
['insert1', 'b', 'c']
>>> len(list2)
3
>>> del list2
>>> list2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'list2' is not defined
remove() 删除具有指定值的元素的第一个匹配项
>>> list1 = [1,1,2,3,4,5]
>>> list1.remove(1)
>>> list1
[1, 2, 3, 4, 5]
pop() 获取并删除指定位置的元素,默认返回偏移量为-1对应的元素,即为pop(-1)
>>> list1 = [1,2,3,4,5,6,7,8]
>>> list1.pop()
8
>>> list1
[1, 2, 3, 4, 5, 6, 7]
>>> list1.pop(0)
1
>>> list1
[2, 3, 4, 5, 6, 7]
index()?找出某个值第一个匹配项的索引位置
>>> list1 = [1,2,3,4,5,6,7,8]
>>> list1.index(3)
2
in 判断某个值是否存在列表中
>>> list2 = ['a','b','c']
>>> 'b' in list2
True
>>> 'bb' in list2
False
count() 记录特定值出现的次数
>>> list1 = [1,2,3,4,5,6,6,6,6,6,6]
>>> list1.count(1)
1
>>> list1.count(6)
6
>>> list1.count("")
0
join() 将列表转换为字符串
>>> note2 = ['hello', 'my', 'first name', 'is', 'Tom']
>>> str1 = "*"
>>> note3 = str1.join(note2)
>>> note3
'hello*my*first name*is*Tom'
>>> note3.split(str1)
['hello', 'my', 'first name', 'is', 'Tom']
sort() 对原列表元素进行排序,改变原列表内容
sorted() 返回排好序的列表副本,不改变原列表内容
如果列表中元素都是数字,则默认从大到小升序排序;如果列表中元素都是字符串,则按照字母表顺序排序;想要变成降序,需要添加参数? reverse = True。
>>> list1 = [4,2,3,5,76,7,1]
>>> sorted(list1)
[1, 2, 3, 4, 5, 7, 76]
>>> sorted(list1,reverse = True)
[76, 7, 5, 4, 3, 2, 1]
>>> list1
[4, 2, 3, 5, 76, 7, 1]
>>> list1.sort()
>>> list1
[1, 2, 3, 4, 5, 7, 76]
>>> list1.sort(reverse = True)
>>> list1
[76, 7, 5, 4, 3, 2, 1]
>>> list2 = ['hello','a','m','ban']
>>> sorted(list2)
['a', 'ban', 'hello', 'm']
>>> list2
['hello', 'a', 'm', 'ban']
>>> sorted(list2,reverse = True)
['m', 'hello', 'ban', 'a']
>>> list2
['hello', 'a', 'm', 'ban']
>>> list2.sort()
>>> list2
['a', 'ban', 'hello', 'm']
>>> list2.sort(reverse = True)
>>> list2
['m', 'hello', 'ban', 'a']
len() 获取长度
>>> list2
['m', 'hello', 'ban', 'a']
>>> len(list2)
4
= 赋值
如果将一个列表赋值给多个变量,改变任意一处其他变量内容也会被修改
>>> list1 = [1,2,3,4]
>>> list2 = list1
>>> list1[0] = "hhhh"
>>> list1
['hhhh', 2, 3, 4]
>>> list2
['hhhh', 2, 3, 4]
>>> list2[1] = "aaa"
>>> list1
['hhhh', 'aaa', 3, 4]
>>> list2
['hhhh', 'aaa', 3, 4]
copy() 复制
复制后的b、c是自身带有值的新对象,不受a的改变的影响
>>> a = [1,2,3]
>>> b = a.copy()
>>> b
[1, 2, 3]
>>> c = a[::]
>>> c
[1, 2, 3]
>>> a[0] = "change"
>>> a
['change', 2, 3]
>>> b
[1, 2, 3]
>>> c
[1, 2, 3]
元组
元组是不可变的,被定义后无法 增加、修改、删除元素;
使用()创建,创建元组时,每个元素后都应该有 ,逗号,只有一个元素也要有,逗号,包含多个元素时,最后一个元素后面的逗号可以省略。
>>> tuple1 = ()
>>> tuple1
()
>>> tuple1 = ("one",)
>>> tuple1
('one',)
>>> tuple2 = ("one","two")
>>> tuple2
('one', 'two')
元组解包
>>> tuple3 = (1,2,3,4)
>>> a,b,c,d = tuple3
>>> a
1
>>> b
2
>>> c
3
>>> d
4
元组实现多个变量值互换,不借助其他变量
>>> a = 1
>>> b = 2
>>> c = 3
>>> a,b,c = c,a,b
>>> a
3
>>> b
1
>>> c
2
tuple() 将其他数据类型转换为元组
>>> a = "aaa"
>>> tuple(a)
('a', 'a', 'a')
>>> a = [1,2,3]
>>> tuple(a)
(1, 2, 3)
>>> b = ["tom","and","jerry"]
>>> tuple(b)
('tom', 'and', 'jerry')
|