#### 表的共享与拷贝 ### 说明表的可变性 ##a1 = a2 = [1, 2, 3] ##a1[2] = "a" ##print(a1) ##print(a2) ### 表对象的共享和变动性 ##x = y = [1, 3, 6] ##z = x ##w = [1, 3, 6] ## ##x[2] = 10 ### 对形参赋值并不影响实参 ##def f(x): ## x = "bbb" ## ##u = "aaa" ##f(u) ##print(u) ### 在函数里通过形参修改实参表的元素, ### 将实际修改在调用时作为实参传给函数的表对象 ##def g(x): ## x[1] = 5 ## ##v = [1, 2, 3] ##print(v) ##g(v) ##print("After call g:", v) # 反转表中元素的函数,实际修改作为实参的表 # Note: i takes value from 0, then -i - 1 ##def rev(lst): ## if not isinstance(lst, list): ## return ## for i in range(len(lst)//2): ## lst[i], lst[-i-1] = lst[-i-1], lst[i] ## ##a1 = [1, 2, 3, 4, 5, 6, 7] ##a2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ## ##print("Original:", a1) ##rev(a1) ##print("After rev:", a1) ##print("Original:", a2) ##rev(a2) ##print("After rev:", a2) ##### 表的应用 ##### 在表中积累可能重用的信息,计算并存储技术 def fib(n): fibs = [1,1] + [0] * (n - 1) # 初始表,两项 fib1 和 fib2 def fib0(k): if fibs[k] != 0: return fibs[k] fibs[k] = fib0(k-1) + fib0(k-2) return fibs[k] return fib0(n) #### 一组高阶表操作,以不同方式处理表中所有元素 ##def map(fun, lst): ## newl = [] ## for x in lst: ## newl.append(fun(x)) ## return newl def reduce(fun, lst, init): acc = init for x in lst: acc = fun(acc, x) return acc ##def do_each(op, lst): ## for i in range(0, len(lst)): ## lst[i] = op(lst[i]) ## ## ##def filter(pred, lst): ## res = [] ## for x in lst: ## if pred(x): ## res.append(x) ## return res ## ## ##def double(x): ## return x*x #### 给英文单词加 er 结尾 #### def word_er(word): if word[-1] == 'e': return word + 'r' if word[-1] == 'y': return word[:-1] + 'ier' return word + 'er' verbs = ["compute", "work", "supply", "walk", "write"] words = map(word_er, verbs) #### 计算前 n 个 Fibonacci 数中所有整除 3 的数之和 def sum_fibs(n): return reduce(lambda x, y: x + y, filter(lambda x: x % 3 == 0, map(fib, list(range(n)))), 0) text1 = """Python was created in the early 1990s by Guido van Rossum at Stichting Mathematisch Centrum (CWI, see http://www.cwi.nl/) in the Netherlands as a successor of a language called ABC. Guido remains Python’s principal author, although it includes many contributions from others. In 1995, Guido continued his work on Python at the Corporation for National Research Initiatives (CNRI, see http://www.cnri.reston.va.us/) in Reston, Virginia where he released several versions of the software. In May 2000, Guido and the Python core development team moved to BeOpen.com to form the BeOpen PythonLabs team. In October of the same year, the PythonLabs team moved to Digital Creations (now Zope Corporation; see http://www.zope.com/). In 2001, the Python Software Foundation (PSF, see http://www.python.org/psf/) was formed, a non-profit organization created specifically to own Python-related Intellectual Property. Zope Corporation is a sponsoring member of the PSF.""" print(text1.split()) ### ##oplist = ['+', '-', '*', '/'] ## ##while True: ## line = input("Exp to calculate: ") ## if line == "quit": ## break ## exp = line.split() ## if len(exp) != 3: ## print("Exp should be 'x op y'.\nTry again, or 'quit' to stop.") ## continue ## op = exp[1] ## if op not in oplist: ## print("Operator is not correct.") ## continue ## x = float(exp[0]) ## y = float(exp[2]) ## if op == '+': ## z = x + y ## elif op == '-': ## z = x - y ## elif op == '*': ## z = x * y ## elif op == '/': ## if y == 0: ## print("Error: divider is 0.") ## continue ## z = x / y ## ## print("Result: ", z)