Python 学习错误笔记

记录自己学习的错误

eg1:

def f(x):	
	return x*x
>>>u=map(f,'123456')
>>> list(u)

  这是讲、以此提取'123456'并且算平方,但是可以发现程序出错。

这里修正后的程序:

>>> def f(x):
x=int(x)
return (x*x)

>>> u=map(f,'123456')
>>> list(u)
[1, 4, 9, 16, 25, 36]

  总结:map依次传给f函数的值是字符串形式的1,2,3,4,5,6,我们需要把他转换为int型,才可以运算

原文地址:https://www.cnblogs.com/liyulongBlog/p/8119031.html