0

我不知道这个问题是否在某个地方被问到,但我没有找到它。

我将元素附加到列表中。

thr_core0 +=[fpid[thread]]

这会定期发生。在时间 0:

thr_core0 [9886, 9890]

在时间 1:

thr_core0 [9886, 9890, 9886, 9890]

是否可以将列表的长度限制为 2。

我知道可以使用deque. 但是否也可以使用lists.

使用deque,我们这样做:

thr_core0 += [deque([0]*2,maxlen=2)]

这些是我在google上搜索的关键字:limit list length python

4

1 回答 1

2

它不漂亮,但你可以切片它:

thr_core0 = (thr_core0 + [fpid[thread]])[:2]

这将始终确保the_core0最多有两个元素。

于 2013-02-26T07:04:49.080 回答