CS 61A 2020 Fall Disc 06: Nonlocal, Mutability, Iterators 待补充

本文深入探讨Python编程中的非局部性、变量可变性和迭代器的使用。通过实例解析如何创建返回更新数的函数、理解列表的内存表示、实现自定义分组函数及列表修改技巧。同时,介绍了filter生成器函数的实现以及有序无重复元素合并的generator。学习这些概念将有助于提升Python编程能力。

1.1 Write a function that takes in a number n and returns a one-argument function. The returned function takes in a function that is used to update n. It should return the updated n.

 

def memory(n): """
        >>> f = memory(10)
        >>> f(lambda x: x * 2)
        20
        >>> f(lambda x: x - 7)
        13
        >>> f(lambda x: x > 5)
        True
        """
    def f(g):
        nonlocal n
        n = g(n)
        return n
    return f

2.1 What would Python display? In addition to giving the output, draw the box and pointer diagrams for each list to the right. 

>>> s1 = [1, 2, 3]
>>> s2 = s1

>>> s1 is s2
True

>>> s2.extend([5, 6])

>>> s1[4]
6

>>> s1.append([-1, 0, 1])

>>> s2[5]
[-1, 0, 1]
    
>>> s3 = s2[:]
>>> s3.insert(3, s2.pop(3)) 
#在s3 range的3位插入s2弹出的3位,此时s3 = [1,2,3,5,5,6,[-1,0,1]]	
   
>>> len(s1)
5
    
>>> s1[4] is s3[6]
True
    
>>> s3[s2[4][1]]
1

>>> s1[:3] is s2[:3]
False
    
>>> s1[:3] == s2[:3]
True

2.2 Fill in the lines belo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值