作业
Topic source
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#方法三:利用zip函数
def triangles():
a = [1]
while True:
yield a
a = [sum(i) for i in zip([0] + a, a + [0])]
n = 0
for t in triangles():
print(t)
n=n+1
if n == 10:
break
#这里用到了zip函数,zip函数基本运作方式:
#>>>x = [1, 2, 3]
#>>>y = [4, 5, 6]
#>>>z = [7, 8, 9]
#>>>zip(x, y, z)
#[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
#>>>x = [1, 2, 3]
#>>>y = [4, 5, 6, 7]
#>>>zip(x, y)
#[(1, 4), (2, 5), (3, 6)]
参考:http://www.cnblogs.com/frydsh/archive/2012/07/10/2585370.html
方法3:利用zip函数,在调用的时候会提示: TypeError: 'int' object is not callable 说sum()只能作用于对int对象。但我在交互环境下测试却是OK的:
a = (1,2) b = (3,4) c = [sum(i) for i in zip(a, b)] print(c) [4, 6]
以上是什么情况原因啊?如何解决呢?
- 1
早起的达先生
整理了一下,自己能力有限,非原创