我有一个由Matlab生成的包,用于python。输出是这样的:
print(IdOut, 'len : ', len(IdOut))
# [[261.0],[263.0],[269.0],[274.0],[328.0],[769.0],[770.0],[772.0],[773.0],[774.0],[1959.0],[1962.0],[3189.0],[3542.0],[4385.0],[4422.0],[4780.0],[5148.0],[5546.0],[5967.0],[6188.0],[6480.0],[6533.0],[6776.0],[6778.0],[6779.0],[7011.0],[7012.0],[7978.0],[8244.0],[8245.0],[8246.0],[8247.0],[8248.0],[10262.0],[10606.0],[11036.0],[11896.0],[12049.0],[13355.0],[13356.0],[13526.0],[13805.0],[13921.0],[13962.0],[13965.0],[15264.0],[15535.0],[15795.0],[15950.0],[16411.0],[16414.0],[16415.0],[17265.0],[17772.0],[17853.0],[18072.0],[19098.0],[19373.0],[21392.0],[21787.0],[22531.0],[22849.0],[23126.0],[23127.0],[23128.0],[23674.0],[23691.0],[26278.0],[26923.0],[26933.0],[27441.0],[27442.0],[27705.0],[27708.0],[27710.0],[28172.0],[28294.0],[28297.0],[28934.0],[28944.0],[29019.0],[29265.0],[29270.0],[30825.0],[31939.0],[32072.0],[32605.0],[33085.0],[33397.0],[33398.0],[33612.0],[33615.0],[33620.0],[33871.0],[34502.0],[34930.0],[35500.0],[35977.0],[36137.0],[36197.0],[36564.0],[36579.0],[36801.0],[36802.0],[36803.0],[37154.0],[37714.0],[37744.0],[37845.0],[37852.0],[38186.0],[38365.0],[38366.0],[38497.0],[38508.0],[38555.0],[38556.0],[38560.0],[38571.0],[38572.0],[38573.0],[39400.0],[39401.0],[39702.0],[39703.0],[39704.0],[40110.0],[40385.0],[40573.0],[40796.0],[41625.0],[42124.0],[42376.0],[43095.0],[43207.0],[43219.0],[43449.0],[43598.0],[43847.0],[44298.0],[44950.0],[45033.0],[45242.0],[45267.0],[46017.0],[46264.0],[47060.0],[47427.0],[47545.0],[48024.0],[48836.0],[48935.0],[49231.0],[49232.0],[50277.0],[50338.0],[50584.0]] len : 158
当我询问IdOut
的类型时,答案是:<class 'mlarray.double'>
其中一个的类型,例如IdOut[0]
是:<class 'mlarray.double'>
>>> type(IdOut)
# <class 'mlarray.double'>
>>> type(IdOut[0])
# <class 'mlarray.double'>
>>> idout[0]
# matlab.double([261.0])
我的问题是,我希望这些数字是整数或浮点数。当我尝试时,我会出现以下错误:
>>> float(IdOut[0])
# Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
float(IdOut[0])
TypeError: float() argument must be a string or a number, not 'double'
>>> int(matlab.double([39400.0]))
# Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
int(matlab.double([39400.0]))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'double'
有人能告诉我如何将mlarray.double
的类型转换为浮点或整数吗?任何帮助都将不胜感激。
您可以使用numpy.array
:
from numpy import array
a = array(IdOut)
print(a[0])
print(int(a[0]))
输出:
array([261.])
261
我不知道这是否有帮助。但我可以说,如果您将mlarray.double
转换为numpy
库中的数组,那么您可以按照自己想要的方式使用它。
import numpy as np
IdOut = np.array(IdOut)
现在你可以用整数列出它:
IdOut_list = [IdOut[i] for i in range(0, len(IdOut))]