python2 和python3 关于字典的差异

2021-07-25  本文已影响0人  mumu不知

Q:

top_10 = np.percentile(dictionary.values(), 90)

TypeError: unsupported operand type(s) for *: 'dict_values' and 'float'

A:

在 python3 中,dict.values返回一个dict_values对象,它不是 alist或tuple。尝试将其强制为列表。

a = list(dictionary.values())

top_10 = np.percentile(a, 90)

The class is written in Python 2, where Dict.values() returns a list, but this was updated in Python 3 to return a dictionary view, described here: https://docs.python.org/3/library/stdtypes.html#dict-views

上一篇 下一篇

猜你喜欢

热点阅读