Python 3.x的字典视图对象和matplotlib [英] Python 3.x's dictionary view objects and matplotlib

查看:201
本文介绍了Python 3.x的字典视图对象和matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python 3.x keys()values()items()中返回优势,但它们似乎也引起了一些兼容性问题.例如,使用matplotlib(最终使用numpy).例如,关于stackexchange问​​题的答案在python 2.x上可以正常工作,但是在python 3.4中执行它们时会引发异常.

In python 3.x keys(), values() and items() return views. Now while views certainly have advantages, they also seem to cause some compatibility issues. For example with matplotlib (ultimately it's with numpy). As an example this and this answers on stackexchange questions work just fine with python 2.x but raise an Exception when executing them in python 3.4.

一个最小的例子是:

import matplotlib.pyplot as plt
d = {1: 2, 2: 10}
plt.scatter(d.keys(), d.values())

使用python 3.4引发TypeError: float() argument must be a string or a number, not 'dict_values'.

Which raises TypeError: float() argument must be a string or a number, not 'dict_values' with python 3.4.

在最小示例中,异常非常清楚, 这个问题是由于相同的问题而引起的,这里的异常要少得多:TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

While for the minimal example the Exception is quite clear, this question arises because of the same problem and here the Exception is a lot less clear: TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

处理此问题的最佳实践是什么?我们是否可以希望在新版本的matplotlib(或最终为numpy)中解决此问题,还是应该在使用matplotlib时开始写list(dict.values())之类的东西,以确保不会遇到该问题? python 3.x有麻烦吗?

What is the best practice to deal with this issue? Can we hope that in a new release of matplotlib (or ultimately numpy) this issue will be dealt with or should we just start to write things like list(dict.values()) when using matplotlib just to be sure not to run into trouble with python 3.x?

推荐答案

该错误的更多信息:

--> 512     return array(a, dtype, copy=False, order=order, subok=True)
    513 
    514 def ascontiguousarray(a, dtype=None):

TypeError: float() argument must be a string or a number, not 'dict_values'

所以最小的例子是:

np.array(d.keys(),dtype=float)

没有dtype规范

In [16]: np.array(d.keys())
Out[16]: array(dict_keys([1, 3]), dtype=object)

dict_keys被视为object.通常,您必须努力避免np.array将对象视为数字列表.

The dict_keys is treated as an object. Usually you have to work at keeping np.array from treating an object as a list of numbers.

In [17]: np.fromiter(d.keys(),dtype=float)
Out[17]: array([ 1.,  3.])

np.fromiter可以处理d.keys(),将其视为可迭代的.因此,在fromiter如何处理与np.array不同的可迭代对象方面有一些细节.

np.fromiter can handle d.keys(), treating it as a iterable. So there's some detail in how fromiter handles an iterable that is different from np.array.

生成器表达式的工作方式相同,例如(i for i in range(4)). fromiter可以遍历它,array要么将其视为对象,要么引发错误.

A generator expression works the same way, e.g. (i for i in range(4)). fromiter can iterate through it, array either treats it as an object or raises an error.

如果SO提到的所有错误归结为处理生成器的np.array(...),则可能可以通过更改numpy来修复该行为.开发人员当然不希望调整可能接受列表的所有函数和方法.但这感觉上是根本性的变化,必须进行彻底的测试.即便如此,它仍然可能产生向后兼容性问题.

If all the errors that the SO mentioned boiled down to np.array(...) handling a generator, then it might be possible to fix the behavior with one numpy change. Developers certainly wouldn't want to tweak every function and method that might accept a list. But it feels like a fundamental change that would have to be thoroughly tested. And even then it's likely to produce backward compatibility issues.

一段时间以来,公认的修复方法是通过2to3传递代码.

The accepted fix, for some time now, has been to pass your code through 2to3.

https://docs.python.org/2/library/2to3.html

对于字典:

修复字典迭代方法. dict.iteritems()转换为dict.items(),dict.iterkeys()转换为dict.keys(),dict.itervalues()转换为dict.values().同样,dict.viewitems(),dict.viewkeys()和dict.viewvalues()分别转换为dict.items(),dict.keys()和dict.values().还会在对列表的调用中包装dict.items(),dict.keys()和dict.values()的现有用法.

Fixes dictionary iteration methods. dict.iteritems() is converted to dict.items(), dict.iterkeys() to dict.keys(), and dict.itervalues() to dict.values(). Similarly, dict.viewitems(), dict.viewkeys() and dict.viewvalues() are converted respectively to dict.items(), dict.keys() and dict.values(). It also wraps existing usages of dict.items(), dict.keys(), and dict.values() in a call to list.

这篇关于Python 3.x的字典视图对象和matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆