美文网首页
python2 和python3 关于字典的差异

python2 和python3 关于字典的差异

作者: mumu不知 | 来源:发表于2021-07-25 16:33 被阅读0次

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

相关文章

网友评论

      本文标题:python2 和python3 关于字典的差异

      本文链接:https://www.haomeiwen.com/subject/pnolfctx.html