Pythonエラーコレクション

9063 ワード

質問1:AttributeError: 'dict' object has no attribute 'iteritems'
AttributeError: 'dict' object has no attribute 'iteritems'

解決1:
Python3.x :iteritems  items

質問2:TypeError: 'range' object doesn't support item deletion
TypeError: 'range' object doesn't support item deletion

解決2:
#     
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

解決3:
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'

解決4:
リストを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]) #list

np.array(a)   # a   numpy array

a.tolist()    # a   python list

質問5:TypeError: %d format: a number is required, not listTypeError: %d format: a number is required, not list
解決5:
#If you use the newer string formatting, there is no need to specify the type.
TypeError: float() argument must be a string or a number, not 'list'
# old
old_method = "%d,%d,%d" % (1, 2, 3)

# new (2.7)
new_method = "{},{},{}".format(1, 2, 3)

質問6:
Python3.6の数値とリストの数値の比較
解決6:
method1:
import operator
a = 5
b = list(range(5))
print(all([operator.gt(a, i) for i in b]))

結果:
True

method2:
a = 5

b = list(range(5))

print(a==b[5])
Traceback (most recent call last):

  File "", line 1, in 
    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

解決7:
説明:IndentationError:インデントブロックが予想されます
インデントを表示して解決します.
質問8:Python UnicodeDecodeError: 'gbk' codec can't decode byte 0xe9Pythonファイル読み込みエラー
Python UnicodeDecodeError: 'gbk' codec can't decode byte 0xe9

解決8:
Python 3.5バージョンの符号化を使用する過程で、直接open(filename,’r’)は、Python UnicodeDecodeError:‘gbk’codec can’t decode byte 0 xe 9がPython 2を使用する.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()  #if status =!200 except
        r.encoding = "utf-8"  #encoding change to utf-8
        return r.text
    except:
        return ""
url = "http://www.zuihaodaxue.cn/zuihaodaxuepaiming2018.html"
print(getHTMLText(url))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
59-b72e2f1d5033> in ()
     12         return ""
     13 url = "http://www.zuihaodaxue.cn/zuihaodaxuepaiming2018.html"
---> 14 print(getHTMLText(url))

TypeError: getHTMLText() takes 0 positional arguments but 1 was given

解決9:
My getHTMLTextmethod needs ‘self’ as a parameter, since it is a class method and not a function. Adding that should make it work fine.