问题:
- 数列:
- a = a1,a2,a3,·····,an
- b = b1,b2,b3,·····,bn
- 求:
- c = a12+b13,a22+b23,a32+b33,·····+an2+bn3
1.用列表+循环实现,并包装成函数
2.用numpy实现,并包装成函数
3.对比两种方法实现的效率,给定一个较大的参数n,用运行函数前后的timedelta表示。
输入代码:
a = list(range(10)) b = list(range(0,50,5)) c=[] for i in range(len(a)): c.append(a[i]+b[i]) print(a,b,c) def pySum(n): a=list(range(n)) b=list(range(0,5*n,5)) c=[] for i in range(len(a)): c.append(a[i]**2+b[i]**3) return (c) print(pySum(20)) import numpy a = numpy.arange(10) b = numpy.arange(0,50,5) a = b + c print(a,b,c) def pySum(n): a=numpy.arange(n) b=numpy.arange(0,5*n,5) c=a**2+b**3 return (c) print(pySum(10))
运行结果: