MIT公開課: Python 筆記7 列表及可變性,字典,效率

來源:互聯網
上載者:User

標籤:

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

MIT公開課: Python 筆記7 列表及可變性,字典,效率

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.