AttributeError:「dict」オブジェクトにPython3の属性エラーはありません



Attributeerror Dict Object Has No Attribute Error Python3



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

エラー表示

AttributeError: 'dict'オブジェクトに属性 'iteritems'がありません
上記のエラーは、python3にこのプロパティがなく、直接次のように変更されたために発生します。 アイテム あなたはできる:

result = sorted(classCount.items(), key=operator.itemgetter(1), reverse=True)

ナレッジポイント補足

Operator.itemgetter関数



オペレータモジュールが提供するitemmgetter関数は、オブジェクトの寸法のデータを取得するために使用されます。パラメータはいくつかのシリアル番号(つまり、オブジェクトで取得されるデータのシリアル番号)です。以下の例を参照してください。

a = [1,2,3] b=operator.itemgetter(1) //Define function b to get the value of the first field of the object print(b(a)) Output: 2 b=operator.itemgetter(1,0) //Define function b to get the value of the first field and the zero field of the object print(b(a)) Output: (2, 1)

operator.itemgetter関数は値を取りませんが、代わりに、オブジェクトに適用することによって値を取得するために使用できる関数を定義することに注意してください。



辞書items()操作メソッド:

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

結果からわかるように、items()メソッドは、ディクショナリ内の各アイテムをタプルとして受け取り、それをリストに追加して、新しいリストコンテナを形成します。返された結果は、必要に応じて新しい変数に割り当てることができます。この新しい変数はリストデータ型になります。

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

転送元:https://blog.csdn.net/sinat_35512245/article/details/78639317