问题1:
AttributeError: 'dict' object has no attribute 'iteritems'
AttributeError: 'dict' object has no attribute 'iteritems'
Python3.x中:iteritems变为items
问题2:
TypeError: 'range' object doesn't support item deletion
TypeError: 'range' object doesn't support item deletion
del(trainingSet[randIndex])
#检查trainingSet
trainingSet = range(2*minLen)
trainingSet =
list
(range(
2
*minLen));
问题3:
IndexError: list index out of range
IndexError: list index out of range
1、范围问题
value无对应的index,造成越界
解决:
增加临界判断语句,或者采取+-1
2、list为空,当list[0]会出现该错误
3、数据问题(有空行)
如下处理文件时,文件有空行
lenses=[inst.strip().split('\t') for inst in fr.readlines()]
解决:
删除空行。
问题4:
AttributeError: 'list' object has no attribute 'shape'
AttributeError: 'list' object has no attribute 'shape'
list 转换为 matrix
#list转矩阵,矩阵行合并
x = [[1.1,2.1,3.1],[1.2,2.2,3.2],[1.3,2.3,3.3]]
m = np.array(x)
print(m)
Out[1]:
array([[ 1.1, 2.1, 3.1],
[ 1.2, 2.2, 3.2],
[ 1.3, 2.3, 3.3]])
#list转矩阵,矩阵列合并
x = [[1.1,2.1,3.1],[1.2,2.2,3.2],[1.3,2.3,3.3]]
#将其转换为矩阵,表示三行三列,每一行表示一个点的信息
m = np.array(x).T
print(m)
Out[2]:
array([[ 1.1, 1.2, 1.3],
[ 2.1, 2.2, 2.3],
[ 3.1, 3.2, 3.3]])
a=([1,2,3,4])
np.array(a)
a.tolist()
TypeError: %d format: a number is required, not list
TypeError: float() argument must be a string or a number, not 'list'
old_method = "%d,%d,%d" % (1, 2, 3)
new_method = "{},{},{}".format(1, 2, 3)
Python3.6中比较数字和列表中的数字
method1:
import operator
a = 5
b = list(range(5))
print(all([operator.gt(a, i) for i in b]))
method2:
a = 5
b = list(range(5))
print(a==b[5])
Traceback (most recent call last):
File "<ipython-input-93-db5156b1d1f6>", line 1, in <module>
print(a==b[5])
IndexError: list index out of range
有错? 不要忘了 list from 0
print(a==b[4])
False
问题7 :
IndentationError: expected an indented block
IndentationError: expected an indented block
解释:IndentationError:预计有一个缩进块
查看缩进,即可解决。
问题8 :
Python UnicodeDecodeError: 'gbk' codec can't decode byte 0xe9
Python文件读取报错
Python UnicodeDecodeError: 'gbk' codec can't decode byte 0xe9
在使用Python 3.5版本编码的过程中,直接open(filename,’r’),总是报错:Python UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xe9但是在用Python2.7版本的时候却什么错误都没有
解决方案:
在头文件中增加import codecs
然后将open(filename,’r’),改成:codecs.open(filename,’r’,encoding=’iso-8859-15’)问题得到解决
问题9:
getHTMLText() takes 0 positional arguments but 1 was given
import requests
def getHTMLText(self):
try:
r = requests.get(url, timeout=30)
r.raise_for_status()
r.encoding = "utf-8"
return r.text
except:
return ""
url = "http://www.zuihaodaxue.cn/zuihaodaxuepaiming2018.html"
print(getHTMLText(url))
TypeError Traceback (most recent call last)
<ipython-input-59-b72e2f1d5033> in <module>()
12 return ""
13 url = "http://www.zuihaodaxue.cn/zuihaodaxuepaiming2018.html"
TypeError: getHTMLText() takes 0 positional arguments but 1 was given
My getHTMLTextmethod needs ‘self’ as a parameter, since it is a class method and not a function. Adding that should make it work fine.
在cmd里面输入pip install tensorflow==2.0, 安装上这个版本的tensorflow再次运行代码就没有问题了。tensorflow高版本在一些程序里面可能会出现一些很奇怪的
错误
,导致升级前可运行的程序,升级后运行不了。在运行
python
环境下的深度学习框架keras的程序时,,运行到语句。
AttributeError: 'dict' object has no attribute 'iteritems'
之所以会出现上述
错误
是因为
python
3中已经没有这个属性,直接改为items即可:
result...
numpy.array可使用 shape。list不能使用shape。
可以使用np.array(list A)进行转换。
(array转list:array B B.tolist()即可)
补充知识:Pandas使用DataFrame出现
错误
:AttributeError: ‘list’ object has no attribute ‘astype’
在使用Pandas的DataFrame时出现了
错误
:AttributeError: ‘list’ object has no attribute ‘astype’
代码入下:
import pandas as pd
pop = {'Neva