python实现KNN算法中遇到的问题

本文针对Python编程中常见的错误进行了详细的解析,包括缩进问题、属性错误及print函数格式规范等,特别关注从Python 2.x到3.x版本迁移时可能遇到的问题。

1.缩进问题
IndentationError:uindent does not match any outer indentation level
错误原因:代码中添加了许多注释,打乱了缩进
解决办法:创建一个空白文件,不附带注释的重写代码,即好

2.属性错误
AttributeError:’dict’ object has no attribute ‘iteritems’
这个异常也是由于python3.x与python2.x的差别造成
2.1python3.x中dict methods dict.keys(), dict.items() and dict.values() return “views” instead of lists.
For example, this no longer works: k = d.keys(); k.sort(). Use k = sorted(d) instead (this works in Python 2.5 too and is just as efficient).
2.2the dict.iterkeys(), dict.iteritems() and dict.itervalues() methods are no longer supported.

3.print()函数格式规范
python3.x相较于python2.x有很多变化:
python3.x使用 print( )function 代替 print satatement have been used in python2.x
eg:print x——>print(x)
print (x,y)——>print((x,y))
本以为是print( )格式规范出错,结果是python版本更新的兼容问题,下述博文中详细总结了print()格式规范。   
http://blog.youkuaiyun.com/jcjc918/article/details/9354815
注:官方给出的python3中的改进部分“What’s New In Python 3.0”
https://docs.python.org/release/3.1.3/whatsnew/3.0.html

4.魔方变量_magic
以下网址中有详尽的解释。http://www.jianshu.com/p/1112320c5784

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值