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]
>>> L1[0] = 4
>>> print L2
[4, 2, 3]
def f(L):
    L[0] = 4
L1 = [1,2,3]
L2 = [1,2,3]
L3 = L1
print L1 == L2
f(L1)
print L1 == L2
print L1
print L2
print L3
#输出
True
False
[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 EtoF
NtoS = {1: 'one', 2: 'two', 'one': 1, 'two': 2}
print NtoS.keys()
print NtoS.keys
del NtoS['one']
print NtoS

L = [['un', 'one'], ['deux', 'two']]
def keySearch(L, k):
    for elem in L:
        if elem[0] == k: return elem[1]
    return None
print keySearch(L, 'deux')

# 输出
football

print 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 base
inputOK = False
while not inputOK:
    base = input('Enter base: ')
    if type(base) == type(1.0):
        inputOK = True
    else:
        print('Error.  Base must be floating point number.')

# Get Height
inputOK = False
while 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 val

base = 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值