简体   繁体   English

Python:限制数组中的元素数

[英]Python: Limiting number of elements in an array

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). 我使用的是The Grinder,我有一个Python脚本,该脚本执行一些Java API来收集最小,最大,执行次数和总执行次数(后两个获得平均执行时间)。 This is done for each API (it's a multidimensional array) and each thread. 这是针对每个API(这是一个多维数组)和每个线程完成的操作。

#Contents of apiTimingsList array: [min, max, number of executions, total execution time]
apiTimingsList = [[9999,0,0,0] for j in range(len(apiList))]

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. 例如,如果我有10个API,并且正在运行900个线程,那么只要测试运行,就会有9000个阵列保持增长。

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? 有没有一种方法来限制这些数组的大小,也就是说只保留最后x次执行,这样我的计算仍然有效,但是这些数组不会失去控制?

You can use collections.deque : 您可以使用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)

来自collections模块的deque可能会完成您想要的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM