我有一个python list
:
[[([1, 20230112060000], [10000, 20230112060000]),
([1, 20230108060000], [7000, 20230109060000]),
([3, 20221229060000], [6929, 20221229060000]),
([1, 20221227060000], [3900, 20221227060000]),
([1, 20221226060000], [6500, 20221226060000]),
([1, 20221221060000], [4400, 20221222060000]),
([1, 20221216060000], [3888, 20221216060000]),
([1, 20221205060000], [5998, 20221205060000]),
([1, 20221128060000], [5000, 20221128060000]),
([1, 20221127060000], [5000, 20221127060000]),
([1, 20221123060000], [5666, 20221123060000]),
([1, 20221122060000], [6000, 20221122060000]),
([1, 20221120060000], [4300, 20221120060000]),
([1, 20221118060000], [4998, 20221118060000]),
([1, 20221028050000], [2700, 20221028050000]),
([1, 20221027050000], [5000, 20221027050000]),
([1, 20221022050000], [4300, 20221022050000]),
([1, 20221019050000], [4498, 20221019050000]),
([1, 20221018050000], [3500, 20221018050000]),
([2, 20221015050000], [3899, 20221015050000]),
([1, 20221011050000], [4500, 20221011050000]),
([2, 20221008050000], [4850, 20221008050000]),
([2, 20221007050000], [5898, 20221007050000]),
([1, 20221004050000], [7499, 20221004050000]),
([1, 20221001050000], [3400, 20221001050000]),
...
[],
[([2, 20230206060000], [357500, 20230206060000])],
[([2, 20230206060000], [357500, 20230206060000]),
([6, 20230205060000], [353833, 20230205060000])],
...]
但是当我尝试将它转换为NumPy数组时,奇怪的事情发生了:
import numpy as np
a = [...] # the above list
b = np.array(a)
b
:
array([list([([1, 20230112060000], [10000, 20230112060000]), ([1, 20230108060000], [7000, 20230109060000]), ([3, 20221229060000], [6929, 20221229060000]), ([1, 20221227060000], [3900, 20221227060000]), ([1, 20221226060000], [6500, 20221226060000]), ([1, 20221221060000], [4400, 20221222060000]), ([1, 20221216060000], [3888, 20221216060000]), ([1, 20221205060000], [5998, 20221205060000]), ([1, 20221128060000], [5000, 20221128060000]), ([1, 20221127060000], [5000, 20221127060000]), ([1, 20221123060000], [5666, 20221123060000]), ([1, 20221122060000], [6000, 20221122060000]), ([1, 20221120060000], [4300, 20221120060000]), ([1, 20221118060000], [4998, 20221118060000]), ([1, 20221028050000], [2700, 20221028050000]), ([1, 20221027050000], [5000, 20221027050000]), ([1, 20221022050000], [4300, 20221022050000]), ([1, 20221019050000], [4498, 20221019050000]), ([1, 20221018050000], [3500, 20221018050000]), ([2, 20221015050000], [3899, 20221015050000]), ([1, 20221011050000], [4500, 20221011050000]), ([2, 20221008050000], [4850, 20221008050000]), ([2, 20221007050000], [5898, 20221007050000]), ([1, 20221004050000], [7499, 20221004050000]), ([1, 20221001050000], [3400, 20221001050000]), ([1, 20220928050000], [5000, 20220929050000]), ([1, 20220926050000], [3000, 20220926050000]), ([1, 20220925050000], [4500, 20220925050000]), ([1, 20220922050000], [4000, 20220922050000]), ([1, 20220920050000], [5000, 20220920050000]), ([1, 20220916050000], [8000, 20220916050000]), ([2, 20220915050000], [6625, 20220915050000]), ([2, 20220914050000], [4500, 20220914050000]), ([1, 20220903050000], [10000, 20220903050000]), ([1, 20220821050000], [8600, 20220821050000]), ([2, 20220820050000], [37500, 20220820050000]), ([1, 20220819050000], [30000, 20220819050000]), ([2, 20220818050000], [13999, 20220818050000]), ([1, 20220816050000], [4000, 20220817050000]), ([1, 20220815050000], [4000, 20220815050000])]),
list([]), list([([1, 20230112060000], [10000, 20230112060000])]),
...,
list([([1, 20230123060000], [5745, 20230123060000]), ([1, 20230105060000], [13000, 20230105060000]), ([1, 20221228060000], [6000, 20221228060000]), ([2, 20221227060000], [6000, 20221227060000]), ([1, 20221222060000], [8571, 20221222060000]), ([1, 20221218060000], [8250, 20221218060000]), ([1, 20221216060000], [8000, 20221216060000]), ([1, 20221213060000], [7500, 20221213060000]), ([1, 20221210060000], [3500, 20221210060000]), ([1, 20221109060000], [6500, 20221109060000])]),
list([([1, 20230123060000], [5745, 20230123060000]), ([1, 20230105060000], [13000, 20230105060000]), ([1, 20221228060000], [6000, 20221228060000]), ([2, 20221227060000], [6000, 20221227060000]), ([1, 20221222060000], [8571, 20221222060000]), ([1, 20221218060000], [8250, 20221218060000]), ([1, 20221216060000], [8000, 20221216060000]), ([1, 20221213060000], [7500, 20221213060000]), ([1, 20221210060000], [3500, 20221210060000]), ([1, 20221109060000], [6500, 20221109060000]), ([1, 20220909050000], [9999, 20220909050000])]),
list([([1, 20230123060000], [5745, 20230123060000]), ([1, 20230105060000], [13000, 20230105060000]), ([1, 20221228060000], [6000, 20221228060000]), ([2, 20221227060000], [6000, 20221227060000]), ([1, 20221222060000], [8571, 20221222060000]), ([1, 20221218060000], [8250, 20221218060000]), ([1, 20221216060000], [8000, 20221216060000]), ([1, 20221213060000], [7500, 20221213060000]), ([1, 20221210060000], [3500, 20221210060000]), ([1, 20221109060000], [6500, 20221109060000]), ([1, 20220909050000], [9999, 20220909050000]), ([1, 20220901050000], [8444, 20220901050000])])],
dtype=object)
由于某些原因,元组和列表没有正确转换。因此,b
的功能不像普通的NumPy数组,因为所有的项都是对象。我知道我可以检查并将所有的tuples
转换为lists
,但有没有办法强制NumPy正确转换所有的项?
顺便说一句,我所说的正确转换是指:
array([list([()])])
它应转换为:
array([[[]]])
1条答案
按热度按时间j2cgzkjk1#
正如
Wakeme UpNow
所指出的,问题在于列表大小不同,使用NumPy
时需要了解的关键点是,它通过对数据进行一些假设来提高性能,例如:如果你打破了这些前提之一,你会自动失去所有的收益,否则你会从
NumPy
使用,因为它将回落到纯粹的pythonic
行为(即dtype=object
)。深入的
NumPy
讨论可以在here中找到。所以解决这个问题的一个方法是使用数值,数据类型相同,长度相同。
干杯