(1) Dictionary
A dictionary is a mapping relationship: keys (key), values (value), Key-value
How to create a dictionary: Create directly and use the DICT function to create
>>> 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 = [' Tianqi ']
>>>salaries = [8000]
>>>print (dict (zip (names,salaries)))
{' Niuyun ': $, ' linling ': 4500, ' Tianqi ': 8000, ' Wangdachui ': 3000}
The basic operation of the dictionary: adding and deleting changes
>>> 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'] = #添加 >>>ainfo{'Tianqi': 8000,'Fuyun': 1000,'Wangdachui': 3000,'linling': 4500,'Niuyun': 9999}>>>'Mayun' inchAinfo #成员判断False>>>delAinfo #删除字典>>>ainfoTraceback (most recent): File "<stdin>", line 1, in <module>nameerror:name ' ainfo ' are not defined
Formatted string for dictionary:
>>> Ainfo = {'Wangdachui': 3000,'Niuyun': 2000,'linling': 4500,'Tianqi': 8000}>>> forKeyinchAinfo.keys ():Print 'name=%s, salary=%s'%(Key, Ainfo[key]) # % (key) format specifier % Dictionary object name >>>"Niuyun ' s salary is% (Niuyun) s."%Ainfo
"Niuyun's salary is."
The role of the output template
>>> 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." ">>>PrintTemplate%Ainfo
Welcome to the pay wall. Niuyun's salary is.Wangdachui's salary is.
The method of the dictionary
Clear () Fromkeys ()
Get () Has_key ()
Items () Pop ()
SetDefault () Update ()
VALUES () copy ()
(2) Collection: A collection of unordered, non-repeating elements
Variable collection: Set
>>> names = [ " wangdachui Span style= "COLOR: #800000" > ", " niuyun ", " Wangzi ", " Wangdachui ", " linling " , " niuyun " ] >> > Namesset = set (names) >>> namesset
{' Wangzi ', ' Niuyun ', ' Wangdachui ', ' linling '
Immutable collection:frozenset
ASet = set ('hello')print= Frozenset ('Hello ')print(fSet)
{' E ', ' l ', ' h ', ' O '}
Frozenset ({' E ', ' l ', ' h ', ' O '})
Collection comparison and relational operators and collection operations
(3) Data structures commonly used in Python
Ndarray (n-dimensional array)
Series (variable length dictionary)
DataFrame (Data frame)
Import= Np.ones ((3,4))print(xarray)1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]
Ndarray:
Basic data structures in the NumPy
Alias is array
Helps save memory and increase CPU counting time
There are a wealth of functions
Creation and output of Ndarray
from Import *>>> aarray = Array ([])>>> Aarrayarray ([1, 2, 3]) >>> Barray = Array ([(), (4,5,6)])>>> Barrayarray ([[1, 2, 3],[4, 5, 6 ] ])>>> zeros ((2,2)) array ([[0., 0.],[0., 0.]) >>> arange (1,5,0.51., 1.5, 2., 2.5, 3., 3.5, 4., 4.5])
Basic operators for Ndarray
>>> Aarray = Array ([(5,5,5), (5,5,5)])>>> barray = Array ([(2,2,2), (2,2,2)])> >> CArray = Aarray * barray>>> Carrayarray ([[[Ten], ten],[, ten]])
>>> Aarray + = barray>>> aarrayarray ([[7, 7, 7],[7, 7, 7]]) >>> Aarray > 5Array ([[True, True, True],[true, True, True]], Dtype=bool)
Properties and methods of Ndarray
>>> Aarray = Array ([(), (4,5,6 >>> Aarray.shape ( 2, 3 >>> Barray = Aarray.reshape ( 3,2) >>> Barrayarray ([[ 1, 2],[ 3, 4],[ 5, 6]) >>> Aarray.sum () 21>>> aarray.sum (axis = 0) Array ([ 5, 7, 9 >>> aarray.sum (axis = 1) array ([ 6, page])
>>> 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 built-in functions
>>>defFun (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.]
The Ufunc function of Ndarray
import NumPy as NP >>> a = Np.arange (1,5 >>> Aarray ([ 1, 2, 3, 4 >>> b = np.arange (2,6 > >> Barray ([ 2, 3, 4, 5 >>>< Span style= "COLOR: #000000" > Np.add (A, b) array ([ 3, 5, 7, 9 >>> Np.add.accumulate ([2, 3, 8 2, 5, 13]) >>> Np.multiply.accumulate ([2, 3, 8]) array ([ 2, 6, 48 = <ufunc " add ";
Python Beginner Summary (ii)