列表是序列的一种,字符串也是序列的一种,
列表的元素是可以修改的,索引和切片
lst=[‘a’,‘b’,‘c’,‘d’]
lst[1]
‘b’
lst[-1]
‘d’
lst[:3]
[‘a’, ‘b’, ‘c’]
lst[:3:1]
[‘a’, ‘b’, ‘c’]
lst[:3:2]
[‘a’, ‘c’]
lst[::2]
[‘a’, ‘c’]
lst[1]=200lst
[‘a’, 200, ‘c’, ‘d’]
lst2=[‘1’,2,3]
lst+lst2
[‘a’, 200, ‘c’, ‘d’, ‘1’, 2, 3]
列表的增加
lst3
[‘a’, 200, ‘c’, ‘d’, ‘1’, 2, 3]
lst3.append(‘zhangsan’)lst3
[‘a’, 200, ‘c’, ‘d’, ‘1’, 2, 3, ‘zhangsan’]
lst3.insert(0,111)
lst3
[111, ‘a’, 200, ‘c’, ‘d’, ‘1’, 2, 3, ‘zhangsan’]lst3.insert(0,111)
lst3
[111, ‘a’, 200, ‘c’, ‘d’, ‘1’, 2, 3, ‘zhangsan’]
lst3.extend(lst)
lst3
[111, ‘a’, 200, ‘c’, ‘d’, ‘1’, 2, 3, ‘zhangsan’, 111, ‘a’, 200, ‘c’, ‘d’]
lst3.extend(“book”)
lst3
[111, ‘a’, 200, ‘c’, ‘d’, ‘1’, 2, 3, ‘zhangsan’, 111, ‘a’, 200, ‘c’, ‘d’, ‘b’, ‘o’, ‘o’, ‘k’]
列表的删除
lst3
[111, ‘a’, 200, ‘c’, ‘d’, ‘1’, 2, 3, ‘zhangsan’, 111, ‘a’, 200, ‘c’, ‘d’, ‘b’, ‘o’, ‘o’, ‘k’]
kst3
Traceback (most recent call last):
File “”, line 1, in
NameError: name ‘kst3’ is not defined
lst3
[111, ‘a’, 200, ‘c’, ‘d’, ‘1’, 2, 3, ‘zhangsan’, 111, ‘a’, 200, ‘c’, ‘d’, ‘b’, ‘o’, ‘o’, ‘k’]
lst3.pop()
‘k’
lst3
[111, ‘a’, 200, ‘c’, ‘d’, ‘1’, 2, 3, ‘zhangsan’, 111, ‘a’, 200, ‘c’, ‘d’, ‘b’, ‘o’, ‘o’]
lst3.pop(1)
‘a’
lst3
[111, 200, ‘c’, ‘d’, ‘1’, 2, 3, ‘zhangsan’, 111, ‘a’, 200, ‘c’, ‘d’, ‘b’, ‘o’, ‘o’]
lst3.remove(‘b’)
lst3
[111, 200, ‘c’, ‘d’, ‘1’, 2, 3, ‘zhangsan’, 111, ‘a’, 200, ‘c’, ‘d’, ‘o’, ‘o’]
lst3.remove(‘d’)lst3
[111, 200, ‘c’, ‘1’, 2, 3, ‘zhangsan’, 111, ‘a’, 200, ‘c’, ‘d’, ‘o’, ‘o’]
lst3.clear()
lst3
列表的排序
lst=[1,2,8,9,4,5,7]
lst
[1, 2, 8, 9, 4, 5, 7]
lst.sort()
lst
[1, 2, 4, 5, 7, 8, 9]
lst.reverse()
lst
[9, 8, 7, 5, 4, 2, 1]
组装
“”.join(lst)
‘python’
lst
[‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’]
元组 tuple 元组不可变
字典 dict 键:值
type(d)
<class ‘dict’>
d={‘name’:‘python’,‘age’:‘25’,‘ciyt’:‘shanghai’}
d
{‘name’: ‘python’, ‘age’: ‘25’, ‘ciyt’: ‘shanghai’}
c=dict(a=1,b=2,c=8)
c
{‘a’: 1, ‘b’: 2, ‘c’: 8}
集合 set