报错代码:

sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True)

解决办法:

Python3中不再支持iteritems(),将iteritems()改成items()


一、 operator.iteritems()函数介绍

1. 作用:iteritems()函数用于获取对象某一个域的值。

2. 例一:

 a = [1,2,3]
b=operator.itemgetter(1) //定义函数b,获取对象的第1个域的值
print(b(a)) 输出:2

例二:

 b=operator.itemgetter(1,0)   //定义函数b,获取对象的第1个域和第0个域的值
print(b(a)) 输出:(2,1)

二、字典items()操作方法

1. 作用:items()方法是将字典中的每个项分别做为元组,添加到一个列表中,形成了一个新的列表容器

2. 例一:

 x = {'title':'python web site','url':'www.iplaypy.com'}
print(x.items()) 输出:[(‘url’, ‘www.iplaypy.com’), (‘title’, ‘python web site’)]

如果有需要也可以将返回的结果赋值给新变量,这个新的变量就会是一个列表数据类型。

 a=x.items()
print(a)
输出:[(‘url’, ‘www.iplaypy.com’), (‘title’, ‘python web site’)]

机器学习实战:KNN代码报错“AttributeError: 'dict' object has no attribute 'iteritems'”的更多相关文章

  1. facenet pyhton3.5 训练 train_softmax.py 时报错AttributeError: 'dict' object has no attribute 'iteritems'

    报错原因:在进行facenet进行train_softmax.py训练时,在一轮训练结束进行验证时,报错AttributeError: 'dict' object has no attribute ' ...

  2. dnspython模块报错 AttributeError: 'CNAME' object has no attribute 'address'

    有时候用到这个模块的时候会报错 AttributeError: 'CNAME' object has no attribute 'address' 如下所示 [root@ansible ch01]# ...

  3. py+selenium 明明定位不到元素,但却不报错或是报错AttributeError: 'list' object has no attribute 'click'【已解决】

    问题:定位不到元素,但却不报错或者出现报错AttributeError: 'list' object has no attribute 'click' 如图  或者  解决方法:   将”driver ...

  4. AttributeError: 'dict' object has no attribute 'iteritems'

    在python3.6中运行 sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse= ...

  5. 关于flask登录视图报错AttributeError: '_AppCtxGlobals' object has no attribute 'user'

    在一个小程序中写了一个登录视图函数,代码如下: @app.route('/login',methods = ['GET','POST']) @oid.loginhandler def login(): ...

  6. Django2.2报错 AttributeError: 'str' object has no attribute 'decode'

    准备将 Django 连接到 MySQL,在命令行输入命令 python manage.py makemigrations 后报错: AttributeError: 'str' object has ...

  7. python文件名不要跟模块名相同,报错AttributeError: 'module' object has no attribute 'Differ'

    python中的文件都会生成pyc文件,包括模块也是这样,所以调用模块的时候,实际上会调用模块.pyc文件:在这个前提下,如果将文件名命名成跟模块名一样,在同一目录下就会生成一个跟模块名一样的pyc文 ...

  8. 运行pytest,报错"AttributeError: 'module' object has no attribute 'xxx'"

    最近学习pytest被此问题困扰,敲脑壳,实在是不该.百度解决方法一大堆,我的问题怎么也解决不了,来看一下,我是怎么解决的,各位大佬勿喷,只是自己做笔记用,谢谢. 报错信息如下: 网上解决方法是这样的 ...

  9. python报错“AttributeError: 'set' object has no attribute 'items'“

    作为才开始学爬虫的萌新,遇到了一个这样的错,很懵逼 后面到网络到处查看大佬的解决方法,才发现headers的请求头部信息有错误,headers是一个字典,不是字符串,所以报错了 原代码 headers ...

随机推荐

  1. Hibernate多对一关联关系

    两个持久化类.Customer 和 OrderForm Customer 类. package com.zcd.hibernate.manyToOne; public class Customer { ...

  2. CRUD是什么?数据结构、增查删改

    http://blog.csdn.net/penginpha/article/details/6920444 CRUD是指在做计算处理时的增加(Create).查询(Retrieve)(重新得到数据) ...

  3. UVa 1638 - Pole Arrangement(dp)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. BZOJ3312:[USACO]No Change(状压DP)

    Description Farmer John is at the market to purchase supplies for his farm. He has in his pocket K c ...

  5. Cow Relays 【优先队列优化的BFS】USACO 2001 Open

    Cow Relays Time Limit: 1000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Tota ...

  6. The Linux Kernel

  7. Python 学习笔记(十一)Python语句(二)

    For 循环语句 基础知识 for循环可以遍历任何序列的项目,如一个列表或者一个字符串. 语法: for 循环规则: do sth >>> for i in "python ...

  8. day 03 --Haproxy 增加, 删除,查询

    key 知识点:函数的定义, 函数的递归调用, flag 标志位的使用,eval() 函数 #!C:\Program Files\Python35\bin # -*- conding:utf-8 -* ...

  9. Web—02-轻松理解css

    CSS基本语法以及页面引用 CSS基本语法 css的定义方法是: 选择器 { 属性:值; 属性:值; 属性:值;} 选择器是将样式和页面元素关联起来的名称,属性是希望设置的样式属性每个属性有一个或多个 ...

  10. Plugin 'InnoDB' registration as a STORAGE ENGINE failed

    今天在安装mysql时遇到了mysql服务打不开的的情况,通过在cmd中输入MySQL --console,显示错误信息,得到如下情况. 原因是InnoDB初始化异常,也就是是说,卸载mysql的时候 ...