0

我有一个使用 Python 3.2 在我的机器上编写的脚本,由于所涉及的数据文件的大小,我需要在另一台运行版本 2.5 的机器上运行它。当我运行以下将 python 列表转换为 numpy 数组的代码时,出现以下错误

Traceback (most recent call last):
File "count_code2.py", line 119, in <module>
cell_centers()
File "count_code2.py", line 58, in cell_centers
read_file(F)
File "count_code2.py", line 44, in read_file
Xa = numpy.array(X, dtype=float)       #Converts lists to NumPy arrays
ValueError: setting an array element with a sequence.

错误所指的代码位是

Xa = numpy.array(X, dtype=float)       #Converts lists to NumPy arrays
Ya = numpy.array(Y, dtype=float)
Za = numpy.array(Z, dtype=float)

其中 X、Y 和 Z 是以下 Python 列表(测试列表)

X= ['0', '0', '0', '1.5', '0', '0', '0', '0', '0', '0']
Y= ['0', '0', '0', '1', '0', '0', '0', '0', '0', '0']
Z= ['0', '0', '0', '1', '0', '0', '0', '0', '0', '0']

我已经尝试将 dtype 重铸为对象,正如我在类似帖子中所读到的那样,这可以解决问题。它确实阻止了错误被抛出,但是另一个对数组中的数据求和的函数在 python 2.x 机器上返回零,并在 3.2 机器上给出这个错误

File "C:\Documents and Settings\My Documents\Count_Code.py", line 103, in counter
((ymin <= Ya) & (Ya <= ymax))&
TypeError: unorderable types: float() <= str()

我假设 dtype 对象将列表转换为字符串数组,这就是我收到此错误的原因。我想问是否有人可以解释为什么版本的差异会导致错误,以及我能做些什么来解决它。提前致谢

4

0 回答 0