MIT公开课: Python 笔记7 列表及可变性,字典,效率

Lecture 7: Lists and mutability,dictionaries,pseudocode,introduction to efficiency 列表及可变性,,字典,伪代码,效率Lists and mutability 列表及可变性>>> L1 = [1, 2, 3]>>> L2 = L1>>> print L2[1, 2, 3]L2[4, 2, 3]:L[0] = 4L1 = [1,2,3]L2 = [1,2,3]L3 = L1print L1 == L2f(L1)print L1 == L2print L1print L2print L3[4, 2, 3][1, 2, 3][4, 2, 3]dictionaries 字典

3wschool python dictionary

– EtoF = {‘one’: ‘un’, ‘soccer’: ‘football’}print EtoF[‘soccer’]print EtoF[0]print EtoFNtoS = {1: ‘one’, 2: ‘two’, ‘one’: 1, ‘two’: 2}print NtoS.keys()print NtoS.keysdel NtoS[‘one’]print NtoSL = [[‘un’, ‘one’], [‘deux’, ‘two’]]:for elem in L:if elem[0] == k: return elem[1]keySearch(L, ‘deux’)# 输出footballprint EtoF[0]KeyError: 0{‘soccer’: ‘football’, ‘one’: ‘un’}[1, 2, ‘two’, ‘one’]<built-in method keys of dict object at 0x7fc52bf0b1e0>{1: ‘one’, 2: ‘two’, ‘two’: 2}twopseudo code 伪代码

求直角三角形的斜边:

import math# Get baseinputOK = inputOK:base = input(‘Enter base: ‘)if type(base) == type(1.0):inputOK = Trueelse:print(‘Error. Base must be floating point number.’)# Get HeightinputOK = inputOK:height = input(‘Enter height: ‘)if type(height) == type(1.0):inputOK = Trueelse:print(‘Error. Height must be floating point number.’)hyp = math.sqrt(base * base + height * height)print ‘Base: ‘ + str(base) + ‘,height: ‘ + str(height) + ‘, hyp: ‘ + str(hyp)

改进:

:inputOK = inputOK:val = input(requestMsg)if type(val) == type(1.0):inputOK = Trueelse:print(errorMsg)return valbase = getFloat(‘Enter base: ‘, ‘Error: base must be a float’)height = getFloat(‘Enter height: ‘, ‘Error: height must be a float’)hyp = math.sqrt(base * base + height * height)print ‘Base: ‘ + str(base) + ‘,height: ‘ + str(height) + ‘, hyp: ‘ + str(hyp)Efficiency 效率Efficiency – orders of growthchoice of algorithm 算法选择map a problem into a class of algorithms of some efficiency 把问题映射为高效的算法space & time 时间 & 空间how much memory does it take 消耗多少存储空间what is the number of the basic steps needed as a function of the input size 进行计算的方法有几步

random access model 随机存取模型

眼睛可以近视,目光不能短浅。

MIT公开课: Python 笔记7 列表及可变性,字典,效率

相关文章:

你感兴趣的文章:

标签云: