MIT公开课: Python 笔记7 列表及可变性,字典,效率
来源:互联网 发布:skrillex用什么软件 编辑:程序博客网 时间:2024/04/30 07:58
Lecture 7: Lists and mutability,dictionaries,pseudocode,introduction to efficiency 列表及可变性,字典,伪代码,效率
Lists and mutability 列表及可变性
>>> L1 = [1, 2, 3]>>> L2 = L1>>> print L2[1, 2, 3]>>> L1[0] = 4>>> print L2[4, 2, 3]
def f(L): L[0] = 4L1 = [1,2,3]L2 = [1,2,3]L3 = L1print L1 == L2f(L1)print L1 == L2print L1print L2print L3#输出TrueFalse[4, 2, 3][1, 2, 3][4, 2, 3]
dictionaries 字典
3wschool python dictionary
- mutable 可改变的
- not ordered 无序
- generalized indexing 索引
- key ,value pair 键值对 -
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']]def keySearch(L, k): for elem in L: if elem[0] == k: return elem[1] return Noneprint 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}two
pseudo code 伪代码
求直角三角形的斜边:
- input value for base 输入底边 as float
- input value for height 输入高 as float
- sqrt (base**2 + height **2) 计算斜边
- output value in hypotenuse 输出
import math# Get baseinputOK = Falsewhile not inputOK: base = input('Enter base: ') if type(base) == type(1.0): inputOK = True else: print('Error. Base must be floating point number.')# Get HeightinputOK = Falsewhile not inputOK: height = input('Enter height: ') if type(height) == type(1.0): inputOK = True else: 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)
改进:
def getFloat(requestMsg, errorMsg): inputOK = False while not inputOK: val = input(requestMsg) if type(val) == type(1.0): inputOK = True else: 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 growth
- choice 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 随机存取模型
- best case – min
- worst case – max
- expected case – avg
0 0
- MIT公开课: Python 笔记7 列表及可变性,字典,效率
- MIT开发课程-计算机科学及编程导论-列表和可变性、字典、效率简介-笔记
- 第七课 列表和可变性、字典、效率简介
- MIT python 公开课
- MIT公开课: Python 笔记6 二分法,牛顿-拉夫森方法,列表
- MIT麻省理工学院公开课:计算机科学及编程导论 Python 笔记1-3
- MIT公开课:计算机科学及编程导论 Python 笔记4 函数分解抽象与递归
- MIT公开课:计算机科学及编程导论 Python 笔记5 浮点数,逐次逼近法和二分法
- MIT公开课---计算机科学及编程导论
- MIT公开课《算法导论》笔记二:渐近符号、递归及解法
- Python学习笔记 之元组、列表及字典
- MIT线性代数公开课
- MIT公开课《算法导论》笔记一:算法分析
- MIT 公开课:Gilbert Strang《线性代数》课程笔记(汇总)
- MIT公开课 线性代数(1)
- MIT公开课 线性代数(2)
- Python列表,元祖及字典的区别
- Python列表序列及字典的操作
- js字符串加密的几种方法
- 甲亢不能吃什么?
- poj 1469 COURSES
- excel转换成PDF的软件
- VA02修改销售订单的BAPI举例
- MIT公开课: Python 笔记7 列表及可变性,字典,效率
- Python学习笔记(四)
- enumerateObjectsUsingBlock 和 for(... in ...)
- 给表加数据的函数
- linux内存分配详解
- HDOJ-1385Minimum Transport Cost(记录路径且有点权的FLOYD)
- PoC
- 微信公众号开发模式之用户账号绑定
- How Many Equations Can You Find