Python的:限制在数组中元素的个数 [英] Python: Limiting number of elements in an array
问题描述
我使用的磨床和我有一个Python脚本,执行一些的Java API来收集的最小值,最大值,执行数和总执行(后者2中得到的平均执行时间)。这是每一个API(这是一个多维数组),并且每个线程中完成的。
I am using The Grinder and I have a Python script which executes some Java APIs to gather the minimum, maximum, number of executions, and total executions (the latter 2 to get the average execution times). This is done for each API (it's a multidimensional array) and each thread.
#Contents of apiTimingsList array: [min, max, number of executions, total execution time]
apiTimingsList = [[9999,0,0,0] for j in range(len(apiList))]
我调查的一些内存的问题,我认为这阵规模的不断扩大可能是一个问题。作为测试运行它会不断地增长。举例来说,如果我有10 API和我运行900线程,有9000阵列将继续增长,只要测试运行。
I am investigating some memory issues and I think that the growing size of this array might be a problem. It will grow constantly as the test runs. For example, if I have 10 APIs and I am running 900 threads, there are 9000 arrays that will keep growing as long as the test runs.
有没有办法来限制这些数组的大小,说只保留执行的最后x号,以便我的计算仍然是有效的,但数组不增长的失控?
Is there a way to limit the size of these arrays, to say only keep the last x number of executions so my calculations are still valid but the arrays are not growing out of control?
推荐答案
您可以使用 collections.deque
:
>>> from collections import deque
>>> d = deque(maxlen=2)
>>> d.append(3)
>>> d.append(4)
>>> d.append(5)
>>> d
deque([4, 5], maxlen=2)
这篇关于Python的:限制在数组中元素的个数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!