MITx - 6.00.1x 笔记(3) Structured Types

Structured Types

Tuple

  • 如果tuple中只有一个元素,需要加逗号“,”,否则不能被识别为tuple
  • tuple中的元素不可被改变(string也是)
  • 函数返回多个值时可以用tuple

tuples
使用tuple
apply tuples

Lists

# 删去list中元素的3种方法
list.del(list[index]) # 删去list中索引为index的元素
list.pop() # 弹出list中最后一个元素
list.remove(element) # 删去list中的element元素

removeLists

lists和string之间转换
convert_list_to_string

applyList

python list document

list.sort(key=None, reverse=False)
  Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).不返回list
  
sorted(iterable, *, key=None, reverse=False)
  Return a new sorted list from the items in iterable.

list.remove(x)
  Remove the first item from the list whose value is x. It is an error if there is no such item.

list.extend(iterable)
  Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable.
  
指向同一个list:
bindingList  

指向不同的list:
copyList
或者使用clone方法:

chill = cool[:]

mutationAndIteration

lists of function:
applyFuns

Map(映射)

两种映射方式:
map


这里写图片描述


Dictionaries

KeysValuesOfDict

改进fib函数
inefficientFib
Memoization(记忆化)
在计算机科学中,记忆化是一种提高进程运行速度的优化技术。通过储存大计算量函数的返回值,当这个结果再次被需要时将其从缓存提取,而不用再次计算来节省计算时间。记忆化是一种典型的时间存储平衡方案。
memoization

trackingEfficiency
trankEfficiency2
trankEfficiency3
可以看到11405773和65之间的明显差距,运行效率大大提升。

 

posted @ 2018-02-25 23:28  huidan  阅读(218)  评论(0编辑  收藏  举报