1. list
len:获取长度
append,insert:添加
pop:删除
直接用索引查询,如果没有,则报错
用in判断是否存在某元素
>>> classmates = ['Michael', 'Bob', 'Tracy']
>>> classmates
['Michael', 'Bob', 'Tracy']
>>> len(classmates)
3
>>> classmates[0]
'Michael'
>>> classmates[3]
Traceback (most recent call last):
File "<python-input-4>", line 1, in <module>
classmates[3]
~~~~~~~~~~^^^
IndexError: list index out of range
>>> classmates[-1]
'Tracy'
>>> classmates[-2]
'Bob'
>>> classmates[-4]
Traceback (most recent call last):
File "<python-input-7>", line 1, in <module>
classmates[-4]
~~~~~~~~~~^^^^
IndexError: list index out of range
>>>> classmates.append('Adam')
>>> classmates
['Michael', 'Bob', 'Tracy', 'Adam']
>>> classmates.insert(1,'Jack')
>>> classmates
['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']
>>> classmates.pop()
'Adam'
>>> classmates
['Michael', 'Jack', 'Bob', 'Tracy']
>>> classmates.pop(1)
'Jack'
>>> classmates
['Michael', 'Bob', 'Tracy']
>>> classmates[1]='Sarah'
>>> classmates
['Michael', 'Sarah', 'Tracy']
>>> L = ['Apple', 123, True]
>>> classmates.append(L)
>>> classmates
['Michael', 'Sarah', 'Tracy', ['Apple', 123, True]]
>>> L=[]
>>> len(L)
0
2. tuple
tuple一旦初始化就不能修改,所以没有append(),insert()这样的方法。其他获取元素的方法和list是一样的,你可以正常地使用classmates[0],classmates[-1],但不能赋值成另外的元素。
tuple不可变,所以代码更安全。如果可能,能用tuple代替list就尽量用tuple。
>>> t = (1)
>>> t
1
定义的不是tuple,是1这个数!这是因为括号()既可以表示tuple,又可以表示数学公式中的小括号,这就产生了歧义,因此,Python规定,这种情况下,按小括号进行计算,计算结果自然是1。
所以,只有1个元素的tuple定义时必须加一个逗号,,来消除歧义:
>>> t = (1,)
>>> t
(1,)
3. dict
相对于list,dict查询数据较快,dict在内部根据key算出value的存放地址
dict有以下几个特点:
1.查找和插入的速度极快,不会随着key的增加而变慢;
2.需要占用大量的内存,内存浪费多。
而list相反:
1.查找和插入的时间随着元素的增加而增加;
2.占用空间小,浪费内存很少。
增加:直接赋值就行,d[‘Adam’] = 99;如果已经存在,则会覆盖
删除:调用pop‘方法,如果不存在就报错
查询:直接按key值查找,d[‘world’],如果不存在就报错
修改:直接按key值修改,如果不存在就表示增加
判断某元素是否存在:使用 in 关键字,存在返回True,反之False;或者使用get,不存在返回None
key值必须为不可修改的
>>> d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}
>>> d['Michael']
95
# 直接增加
>>> d['Adam'] = 67
>>> d
{'Michael': 95, 'Bob': 75, 'Tracy': 85, 'Adam': 67}
# 覆盖
>>> d['Adam'] = 99
>>> d
{'Michael': 95, 'Bob': 75, 'Tracy': 85, 'Adam': 99}
# 不存在报错
>>> d['John']
Traceback (most recent call last):
File "<python-input-6>", line 1, in <module>
d['John']
~^^^^^^^^
KeyError: 'John'
# 使用in判断
>>> 'Thomas' in d
False
>>> 'Adam' in d
True
# 不存在返回None,交互环境不显示结果
>>> d.get('Thomas')
# 不存在返回默认值-1
>>> d.get('Thomas',-1)
-1
# 删除
>>> d.pop('Bob')
75
>>> d
{'Michael': 95, 'Tracy': 85, 'Adam': 99}
>>> key = [1,2,3]
>>> d[key] = 'list'
Traceback (most recent call last):
File "<python-input-14>", line 1, in <module>
d[key] = 'list'
~^^^^^
TypeError: unhashable type: 'list'
# 不存在就删除会报错
>>> d.pop('hello')
Traceback (most recent call last):
File "<python-input-16>", line 1, in <module>
d.pop('hello')
~~~~~^^^^^^^^^
KeyError: 'hello'
# 不存在获取值也报错
>>> d['world']
Traceback (most recent call last):
File "<python-input-17>", line 1, in <module>
d['world']
~^^^^^^^^^
KeyError: 'world'
4. Set
set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。
创建:直接创建,或者以list作为参数创建
添加:add
删除:remove,如果没有会报错
判断是否存在:in
集合操作:&,交集;|,合集
# 直接创建
>>> s = {1, 2, 3}
>>> s
{1, 2, 3}
# 使用list创建
>>> s = set([4,5,6])
>>> s
{4, 5, 6}
# 添加
>>> s.add(8)
>>> s
{8, 4, 5, 6}
# 删除
>>> s.remove(10)
Traceback (most recent call last):
File "<python-input-6>", line 1, in <module>
s.remove(10)
~~~~~~~~^^^^
KeyError: 10
>>> s.remove(5)
>>> s
{8, 4, 6}
>>> 100 in s
False
>>> s1 = {1,2,3}
>>> s2={2,3,4}
>>> s1&s2
{2, 3}
>>> s1 |s2
{1, 2, 3, 4}

1602

被折叠的 条评论
为什么被折叠?



