MITx - 6.00.1x 笔记(3) Structured Types

本文介绍了Python中tuple和list的基本用法,包括tuple的定义、特性及应用场景,list的操作如增删改查等,并探讨了如何利用这些数据结构进行高效编程。此外,还介绍了记忆化技术在递归函数中的应用,显著提升了程序运行效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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之间的明显差距,运行效率大大提升。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值