目录
一、列表
1、列表的基本知识
>>> type([1,2,3,4,5])
<class 'list'>
#可以在列表中加入字符串、数字、bool类型
>>> ['hello','OMG',1,True,False]
['hello', 'OMG', 1, True, False]
#嵌套列表
>>> [[1,2],[1,2],5,[8,9]]
[[1, 2], [1, 2], 5, [8, 9]]
2、列表的基本操作
1)取值
>>> ['fool','allringht','forwhat'][0:2]
['fool', 'allringht']
>>> ['fool','allringht','forwhat'][-1]
'forwhat'
2)运算
#加法
>>> ['fool','allringht','forwhat']+['go','back']
['fool', 'allringht', 'forwhat', 'go', 'back']
#单数组*数字
>>> ['fool','allringht','forwhat']*3
['fool', 'allringht', 'forwhat', 'fool', 'allringht', 'forwhat', 'fool', 'allringht', 'forwhat']
#单数组*bool
>>>['fool','allringht','forwhat']*True
SyntaxError: invalid syntax
>>> ['fool','allringht','forwhat']*False
[]#和*0的结果相同
#多数组相乘不存在
>>> ['fool','allringht','forwhat']*['go','back']
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
['fool','allringht','forwhat']*['go','back']
TypeError: can't multiply sequence by non-int of type 'list'
#减法不存在
>>> ['fool','allringht','forwhat']-['fool']
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
['fool','allringht','forwhat']-['fool']
TypeError: unsupported operand type(s) for -: 'list' and 'list'
>>>
二、元组
tuple,,操作与列表相似
>>> (1,2,3)
(1, 2, 3)
>>> (1,'fool',True)
(1, 'fool', True)
#元组乘法
>>> (1,2,3)*3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
#元组加法
>>> (1,2,3)+(5,4,7)
(1, 2, 3, 5, 4, 7)
#元组取数
>>> (1,2,3)[2]
3
#元组只有一个元素时会被默认为数,解决:加“,”
>>> type((1))
<class 'int'>
>>> type((1,))
<class 'tuple'>
#空元组
>>> type(())
<class 'tuple'>
>>> type((1,2,3,9))
<class 'tuple'>
>>> type(1)
<class 'int'>
>>> type([1])
<class 'list'>
三、三种类型总结
字符串str、数组list、元组tuple操作有类似之处
in、len()、max、min、ord函数
#in判断元素是否存在
>>> 3 in [1,2,3]
True
>>> 3 not in [1,2,3]
False
#len求长度
>>> len('123456')
6
>>> len([1,2,3,4,5,6])
6
#max、min求最大、最小值
>>> max([1,2,3,4,5,6])
6
>>> min([1,2,3,4,5,6])
1
>>> max('hello world')
'w'
>>> min('helloworld')
'd'
>>> min('hello world')
' '
#ord将单个字符转换为ASCII
>>> ord('w')
119
>>> ord(' ')
32
>>> ord('d')
100
>>>
四、集合
set,重要特征:无序。
>>> type({1,2,3,4,5})
<class 'set'>
>>> len({1,2,3})
3
>>> 1 in {1,2,3,5}
True
>>> 1 not in{12,3,1}
False
#集合操作:差集、交集、并集
>>> {1,2,3,4,5,6}-{3,4}
{1, 2, 5, 6}
>>> {1,2,3,4,5}&{1,5}
{1, 5}
>>> {1,2,3,4,5}|{1,8,5,9}
{1, 2, 3, 4, 5, 8, 9}
#定义空集用set()
>>> type({})
<class 'dict'>
>>> type(set())
<class 'set'>
>>> len(set())
0
五、字典
dict
1、基本格式
>>> #{key1:value1,key2:value2...}
>>> type({1:1,2:2,3:3})
<class 'dict'>
#空字典
>>> type({})
<class 'dict'>
#key可以是int、float、str、tuple,不能是列表list
>>> {1:'lo','1':'big',1.1:'fine'}
{1: 'lo', '1': 'big', 1.1: 'fine'}
>>> {[1,2]:'lo','1':'big',1.1:'fine','r':{1:2}}
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
{[1,2]:'lo','1':'big',1.1:'fine','r':{1:2}}
TypeError: unhashable type: 'list'
>>> {(1,2):'lo','1':'big',1.1:'fine','r':{1:2}}
{(1, 2): 'lo', '1': 'big', 1.1: 'fine', 'r': {1: 2}}
#value可以是字典
>>> {1:'lo','1':'big',1.1:'fine','r':{1:2}}
{1: 'lo', '1': 'big', 1.1: 'fine', 'r': {1: 2}}
2、基本操作
>>> {'q':'lo','w':'big','e':'fine'}[0]
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
{'q':'lo','w':'big','e':'fine'}[0]
KeyError: 0
>>> {'q':'lo','w':'big','e':'fine'}['w']
'big'