(1)字典
字典是一种映射关系:键(key),值(value),key-value对
创建字典的方式:直接创建和利用dict函数创建
>>> aInfo = {'Wangdachui': 3000, 'Niuyun':2000, 'Linling':4500, 'Tianqi':8000} >>> info = [('Wangdachui',3000), ('Niuyun',2000), ('Linling',4500), ('Tianqi',8000)] >>> bInfo = dict(info) >>> cInfo = dict([['Wangdachui',3000], ['Niuyun',2000], ['Linling',4500], ['Tianqi',8000]]) >>> dInfo = dict(Wangdachui=3000, Niuyun=2000, Linling=4500, Tianqi=8000)
>>> aDict = {}.fromkeys(('Wangdachui', 'Niuyun', 'Linling', 'Tianqi'),3000) >>> aDict {'Tianqi': 3000, 'Wangdachui': 3000, 'Niuyun': 3000, 'Linling': 3000}
>>> sorted(aDict)
['Linling', 'Niuyun', 'Tianqi', 'Wangdachui']
>>>names = ['Wangdachui', 'Niuyun', 'Linling', 'Tianqi']
>>>salaries = [3000, 2000, 4500, 8000]
>>>print(dict(zip(names,salaries)))
{'Niuyun': 2000, 'Linling': 4500, 'Tianqi': 8000, 'Wangdachui': 3000}
字典的基本操作:增删改查
>>> aInfo = {'Wangdachui': 3000, 'Niuyun':2000, 'Linling':4500, 'Tianqi':8000} >>> aInfo['Niuyun'] #键值查找 5000 >>> aInfo['Niuyun'] = 9999 #更新 >>> aInfo {'Tianqi': 8000, 'Wangdachui': 3000, 'Linling': 4500, 'Niuyun': 9999} >>> aInfo['Fuyun'] = 1000 #添加 >>> aInfo {'Tianqi': 8000, 'Fuyun': 1000, 'Wangdachui': 3000, 'Linling': 4500, 'Niuyun': 9999} >>> 'Mayun' in aInfo #成员判断 False >>> del aInfo #删除字典 >>> aInfo Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'aInfo' is not defined
字典的格式化字符串:
>>> aInfo = {'Wangdachui': 3000, 'Niuyun':2000, 'Linling':4500, 'Tianqi':8000} >>> for key in aInfo.keys(): print 'name=%s, salary=%s' % (key, aInfo[key]) # %(key)格式说明符 % 字典对象名>>> "Niuyun's salary is %(Niuyun)s." % aInfo
"Niuyun's salary is 5000."
输出模板的作用
>>> aInfo = {'Wangdachui': 3000, 'Niuyun':2000, 'Linling':4500, 'Tianqi':8000} >>> template = ''' Welcome to the pay wall. Niuyun's salary is %(Niuyun)s. Wangdachui's salary is %(Wangdachui)s. ''' >>> print template % aInfo
Welcome to the pay wall. Niuyun's salary is 2000. Wangdachui's salary is 3000.
字典的方法
clear() fromkeys()
get() has_key ()
items() pop()
setdefault() update()
values() copy()
(2)集合:无序不重复的元素的集合
可变集合:set
>>> names = ['Wangdachui', 'Niuyun', 'Wangzi', 'Wangdachui', 'Linling', 'Niuyun'] >>> namesSet = set(names) >>> namesSet
{'Wangzi', 'Niuyun', 'Wangdachui', 'Linling'
不可变集合:frozenset
aSet = set('hello') print(aSet) fSet = frozenset('hello') print(fSet)
{'e', 'l', 'h', 'o'}
frozenset({'e', 'l', 'h', 'o'})
集合比较和关系运算符和集合操作
(3)python常用的数据结构
ndarray(N维数组)
Series(变长字典)
DataFrame(数据框)
import numpy as np xArray = np.ones((3,4)) print(xArray) [[ 1. 1. 1. 1.] [ 1. 1. 1. 1.] [ 1. 1. 1. 1.]]
ndarray:
NumPy中基本的数据结构
别名为array
利于节省内存和提高CPU计算时间
有丰富的函数
ndarray的创建和输出
>>> from numpy import * >>> aArray = array([1,2,3]) >>> aArray array([1, 2, 3]) >>> bArray = array([(1,2,3),(4,5,6)]) >>> bArray array([[1, 2, 3], [4, 5, 6]]) >>> zeros((2,2)) array([[ 0., 0.], [ 0., 0.]]) >>> arange(1,5,0.5) array([ 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])
ndarray的基本运算符
>>> aArray = array([(5,5,5),(5,5,5)]) >>> bArray = array([(2,2,2),(2,2,2)]) >>> cArray = aArray * bArray >>> cArray array([[10, 10, 10], [10, 10, 10]]) >>> aArray += bArray >>> aArray array([[7, 7, 7], [7, 7, 7]]) >>> aArray > 5 array([[ True, True, True], [True, True, True]], dtype=bool)
ndarray的属性和方法
>>> aArray = array([(1,2,3),(4,5,6)]) >>> aArray.shape (2, 3) >>> bArray = aArray.reshape(3,2) >>> bArray array([[1, 2], [3, 4], [5, 6]]) >>> aArray.sum() 21 >>> aArray.sum(axis = 0) array([5, 7, 9]) >>> aArray.sum(axis = 1) array([ 6, 15])
>>> aArray = array([1,3,7])
>>> bArray = array([3,5,8])
>>> cArray = array([9,8,7])
>>> aArray[1:]
array([3, 7])
>>> where(aArray>2, bArray, cArray)
array([9, 5, 8]
ndarray的内建函数
>>> def fun(x,y): return (x+1)*(y+1) >>> arr = fromfunction(fun,(9,9)) >>> arr
array([[ 1., 2., 3., 4., 5., 6., 7., 8., 9.], [ 2., 4., 6., 8., 10., 12., 14., 16., 18.], [ 3., 6., 9., 12., 15., 18., 21., 24., 27.], [ 4., 8., 12., 16., 20., 24., 28., 32., 36.], [ 5., 10., 15., 20., 25., 30., 35., 40., 45.], [ 6., 12., 18., 24., 30., 36., 42., 48., 54.], [ 7., 14., 21., 28., 35., 42., 49., 56., 63.], [ 8., 16., 24., 32., 40., 48., 56., 64., 72.], [ 9., 18., 27., 36., 45., 54., 63., 72., 81.]])
ndarray的ufunc函数
import numpy as np >>> a = np.arange(1,5) >>> a array([1, 2, 3, 4]) >>> b = np.arange(2,6) >>> b array([2, 3, 4, 5]) >>> np.add(a,b) array([3, 5, 7, 9]) >>> np.add.accumulate([2, 3, 8]) array([ 2, 5, 13]) >>> np.multiply.accumulate([2, 3, 8]) array([ 2, 6, 48]) Source help(ufunc) help(numpy) add = <ufunc 'add'>