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

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

836

被折叠的 条评论
为什么被折叠?



