知识点总结:
字典表dict
#dict声明方式1
>>> d = {'ISBN':'23144','Title':'Python'}
#dict获取
>>> d['Title']
'Python'
>>> d
{'ISBN': '23144', 'Title': 'Python'}
#dict赋值
>>> d['Author'] = 'Jerry'
>>> d
{'ISBN': '23144', 'Title': 'Python', 'Author': 'Jerry'}
#dict获取不存在
>>> d['PubDate']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'PubDate'
#dict获取不存在方式get
>>> d.get('Price')
#dict声明方式2
>>> emp = dict(name='Mike',age=20,job='dev')
>>> emp
{'name': 'Mike', 'age': 20, 'job': 'dev'}
>>> emp
{'name': 'Mike', 'age': 20, 'job': 'dev'}
>>> d
{'ISBN': '23144', 'Title': 'Python', 'Author': 'Jerry'}
#dict update操作
>>> emp.update(d)
>>> emp
{'name': 'Mike', 'age': 20, 'job': 'dev', 'ISBN': '23144', 'Title': 'Python', 'Author': 'Jerry'}
#dict pop操作
>>> emp.pop('age')
20
>>> emp
{'name': 'Mike', 'job': 'dev', 'ISBN': '23144', 'Title': 'Python', 'Author': 'Jerry'}
# dict key集合
>>> emp.keys()
dict_keys(['name', 'job', 'ISBN', 'Title', 'Author'])
# dict value集合
>>> emp.values()
dict_values(['Mike', 'dev', '23144', 'Python', 'Jerry'])
# dict item集合(元组)
>>> d.items()
dict_items([('ISBN', '23144'), ('Title', 'Python'), ('Author', 'Jerry')])
# 遍历dict元素
>>> for k,v in emp.items():
... print('{}->{}'.format(k,v))
...
name->Mike
job->dev
ISBN->23144
Title->Python
Author->Jerry
# dict嵌套
>>> emp = {'age':20,'name':{'firstname':'Jerry','lastname':'Lee'}}
>>> emp['name']['lastname']
'Lee'
元组tuple
# tuple 声明方式1
>>> (1,2)
(1, 2)
# tuple 拼接
>>> (1,2)+(3,4)
(1, 2, 3, 4)
# tuple 声明方式2 不带括号
>>> 1,2
(1, 2)
# 这样不是tuple
>>> x = (40)
>>> x
40
# tuple 声明方式3 单值带逗号
>>> x = (40,)
>>> x
(40,)
# tuple 声明方式4 单值带逗号
>>> x = 40,
>>> x
(40,)
>>> len(x)
1
# tuple不可修改
>>> x[0]=99
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
# tuple遍历
>>> x = (1,2,3,4)
>>> for i in x:
... print(i**2)
...
1
4
9
16
# tuple to list
>>> res = []
>>> for i in x:
... res.append(i**2)
...
>>> res
[1, 4, 9, 16]
# tuple支持推导
>>> res = [i**2 for i in x]
>>> res
[1, 4, 9, 16]
# 与list相同的 下标查和计数
>>> x.index(3)
2
>>> x.count(3)
1
# 对象 namedtuple
>>> from collections import namedtuple
>>> Employee = namedtuple('Employee',['name','age','dept','salary'])
>>>
>>> jerry = Employee('Jerry',age=30,dept='DEV',salary=9000.0)
>>> jerry
Employee(name='Jerry', age=30, dept='DEV', salary=9000.0)
# 像对象一样调用属性
>>> jerry.name
'Jerry'
文件 file
def open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True):
Character Meaning
--------- ---------------------------------------------------------------
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' create a new file and open it for writing
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newline mode (deprecated)
========= ===============================================================
# 创建一个文件
>>> f = open('hello.txt','w')
# 创建文件夹用os模块mkdir()方式
>>> os.makedirs(path)
# 写文件
f.write('hello world')
11
f.flush()
#读取文件
>>> f = open('hello.txt', 'r')
>>> f.read()
#读取二进制文件
f = open('data.bin','rb')
# 序列化d 使用pickle
d = {'a':1,'b':2}
f = open('datafile.pkl','wb')
import pickle
pickle.dump(d,f)
f.flush()
f.close()
#反序列化data
f = open('datafile.pkl','rb')
data = pickle.load(f)
data
{'a': 1, 'b': 2}