基础(三)
字典声明操作基本操作需要注意的操作
属性排序
元组特点声明操作面向对象的namedtuple
文件基本语法操作例子:
pickle存取python对象
字典
声明
字典的声明有两种方式
{键:值,,}
>>> d
= {'ISBN':'22222','Title':'Pythonbase','price':30}
>>> d
{'ISBN': '22222', 'Title': 'Pythonbase', 'price': 30}
dict(键=键值,)
>>> d
= dict(ISBN
='22222',Title
='Pythonbase',price
=30)
>>> d
{'ISBN': '22222', 'Title': 'Pythonbase', 'price': 30}
操作
基本操作
填加
>>> d
= {'ISBN':'22222','Title':'Pythonbase','price':'30'}
>>> d
{'ISBN': '22222', 'Title': 'Pythonbase', 'price': '30'}
>>> d
['Author'] = 'xue'
>>> d
{'ISBN': '22222', 'Title': 'Pythonbase', 'price': '30', 'Author': 'xue'}
更改
>>> l
= [1,2,3,4,5]
>>> l
[0]= 99
>>> l
[99, 2, 3, 4, 5]
需要注意的操作
获取
字典名称[键]
>> d
= {'ISBN':'22222','Title':'Pythonbase','price':'30'}
>>> d
{'ISBN': '22222', 'Title': 'Pythonbase', 'price': '30'}
>>> d
['Title']
'Pythonbase'
>>> d
['price']
'30'
字典名称.get(‘键’,没有此键的输出值)———这个操作在项目中用处很大,避免报错
>>> d
{'ISBN': '22222', 'Title': 'Pythonbase', 'price': '30', 'Author': 'xue'}
>>> d
.get
('price')
'30'
>>> d
.get
('Price',0.0)
0.0
合并:字典.update(字典名)
属性
输出键keys
>>> emp
{'name': 'xueling', 'age': 40, 'pubdata': 2019, 'dep': '技术'}
>>> emp
.keys
()
dict_keys
(['name', 'age', 'pubdata', 'dep'])
>>> for i
in emp
.keys
():
... print(i
)
...
name
age
pubdata
dep
输出键值values
>>> emp
{'name': 'xueling', 'age': 40, 'pubdata': 2019, 'dep': '技术'}
>>> emp
.values
()
dict_values
(['xueling', 40, 2019, '技术'])
输出所有内容
>>> emp
.items
()
dict_items
([('name', 'xueling'), ('age', 40), ('pubdata', 2019), ('dep', '技术')])
排序
将keys放入列表
>>> d
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> ks
= list(d
.keys
())
>>> ks
.sort
()
>>> ks
['a', 'b', 'c', 'd']
使用全局函数sorted()
>>> d
= {'b':1,'a':2,'c':3,'d':4}
>>> ks
= d
.keys
()
>>> for k
in sorted(ks
):
... print(k
,d
.get
(k
))
...
a
2
b
1
c
3
d
4
>>>
元组
特点
任意对象,下标访问,不可改变
声明
>>> (1,2)
(1, 2)
操作
加、赋值、交换
>>> (1,2)+(3,4)
(1, 2, 3, 4)
>>> x
= 40
>>> x
40
>>> x
= 5
>>> y
= 10
>>> x
,y
= y
,x
>>> x
10
>>> y
5
查找
>>> t
= (1,2,3)
>>> t
.index
(2)
1
统计
>>> t
= (1,2,3)
>>> t
.count
(3)
1
其他操作
>>> t
= (1,2,3)
>>> for x
in t
:
... print(x
**2)
...
1
4
9
>>> res
=[]
>>> for x
in t
:
... res
.append
(x
**2)
...
>>> res
[1, 4, 9]
>>> res
= [x
**2 for x
in t
]
>>> res
[1, 4, 9]
面向对象的namedtuple
from collections
import namedtuple
Employee
= namedtuple
('Employee',['name','age','job'])
jerry
= Employee
('jerry',30,'computer')
jerry
.name
'jerry'
jerry
.age
30
jerry
.job
'computer'
文件
基本语法
file = open(‘文件名’,mode)
mode含义
r读w写a追加b二进制文件+既读又写
操作
readreadlinereadlines:list遍历closeformat
例子:
>>>ff
= open('hello.txt','w')
>>>ff
.write
('hi\nI am \n')
10
>>>ff
.close
()
>>>f
= open('hello.txt','r')
>>>f
.read
()
'hi\n I am \n'
>>>f
.readline
()
''
>>>l
= open('hello.txt').readlines
()
>>>l
['hi\n', ' I am \n']
>>>for i
in l
:
print(i
)
hi
I am
>>>x
,y
,z
=1,2,3
>>>l
= [1,2,3]
>>>l
[1, 2, 3]
>>>f
= open('datafile.txt','w')
>>>f
.write
('{},{},{}'.format(x
,y
,z
))
5
>>>f
.write
(str(l
))
9
>>>f
.close
()
>>>chars
= open('datafile.txt').read
()
>>chars
'1,2,3\n[1, 2, 3]'
pickle存取python对象
序列化
dump(对象,目标文件)load(文件)
>>>d
= {'a':1,'b':2}
>>>f
= open('datafile.pkl','wb')
>>>import pickle
>>>pickle
.dump
(d
,f
)
>>>f
.close
()
>>>open('datafile.pkl','rb').read
()
b
'\x80\x03}q\x00(X\x01\x00\x00\x00aq\x01K\x01X\x01\x00\x00\x00bq\x02K\x02u.'
>>>f
= open('datafile.pkl','rb')
>>>data
= pickle
.load
(f
)
>>>data
{'a': 1, 'b': 2}