numpy追加到数组 [英] Numpy Append to Array
问题描述
我正在为Raspberry Pi建立一个项目,该项目可以在特定的时间范围内随机打开和关闭继电器.为了管理时间段,我想使用每天生成的二维数组.因此,我的Python应用程序需要清空前一天的数组,并使用随机生成的打开/关闭时隙填充该数组.我不知道如何将我的时间值附加到数组中.有人能帮我吗? 这是我的代码:
I’m building a project for the Raspberry Pi that turns a relay on and off random times in a specific time window. To manage the time slots, I want to use a two-dimensional array that’s generated daily. So, my Python application needs to empty the previous day’s array, and populate it with the on/off time slots being generated randomly. I can’t figure out how to append my time values to the array. Can someone help me? Here’s my code:
daily_slots = np.empty([1], dtype=[('onTime', np.dtype(int)), ('offTime', np.dtype(int))])
# numpy populates the array with whatever is in memory at that time,
# so delete the existing, 'empty' array row
daily_slots = np.delete(daily_slots, 0, 0)
有了这个,如何附加值? numpy文档说我应该执行以下操作:
With that in place, how do I append values? The numpy documentation says I should do something like the following:
daily_slots = np.append(daily_slots, [700, 800])
但这不起作用,我得到
Traceback (most recent call last):
File "./controller.py", line 351, in <module>
init_app()
File "./controller.py", line 128, in init_app
build_daily_slots_array()
File "./controller.py", line 307, in build_daily_slots_array
daily_slots = np.append(daily_slots, [700, 800])
File "/usr/lib/python2.7/dist-packages/numpy/lib/function_base.py", line 3555, in append
return concatenate((arr, values), axis=axis)
TypeError: invalid type promotion
我尝试了所有可以想到的事情,并戳了一下numpy文档,却没有找到答案.
I've tried everything I can think of and I've poked and prodded at the numpy documentation and I've not found the answer.
我没有正确声明数组吗?我希望每个数组行"都包含两个元素的数组(ontime和offtime).
Am I not declaring the array correctly? I want each array 'row' to consist of a two-element array (the ontime and offtime).
推荐答案
np.append
使用np.concatenate
.它不是列表追加的真实副本.它将新值转换为数组并进行连接. 但是要与结构化数组连接,所有数组必须具有相同的dtype (或至少具有兼容的dtype,因此会出现促销"错误.
np.append
uses np.concatenate
. It isn't a true copy of list append. It turns the new value into an array and does concatenate. But to do concatenate with structured arrays, all arrays have to have the same dtype (or at least compatible ones, hence the 'promotion' error.
这有效:
In [4]: np.append(daily_slots, np.array((700, 800), dtype=daily_slots.dtype))
Out[4]:
array([( 0, 1075970048), (700, 800)],
dtype=[('onTime', '<i4'), ('offTime', '<i4')])
这也可以.我必须添加[]
,以便第二个数组像第一个一样是1d.没有它们,它就是0d-实际上就是np.append
添加到游戏中的全部内容.
This also works. I had to add the []
so that the 2nd array was 1d like the first. Without them it is 0d - in effect that's all that np.append
adds to the game.
In [6]: np.concatenate((daily_slots, np.array([(700, 800)], dtype=daily_slots.dtype)))
Out[6]:
array([( 0, 1075970048), (700, 800)],
dtype=[('onTime', '<i4'), ('offTime', '<i4')])
对于增长和收缩,列表通常会更好.在这种情况下,将包含2个元素元组的列表,或者可能是自定义类的列表.
For growing and shrinking, a list is usually better. In this case a list of 2 element tuples, or may be a list of a custom class.
如果它是一个元组列表,则可以将其包装在np.array
中,以将其转换为2d数组,以便于进行计算.
If it is a list of tuples, you could wrap it in np.array
to turn it into a 2d array for ease of doing calculations.
这篇关于numpy追加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!