gpt4 book ai didi

python - 连续替换数组或列表中的值

转载 作者:太空狗 更新时间:2023-10-29 22:03:59 24 4
gpt4 key购买 nike

我是编码的新手,我想知道是否可以将 python 中的数组限制为 100 个项目。

如果可能的话,你能继续向那个数组添加并推出数组中的旧数字吗?因此,每次添加新号码时,都应将最早的号码推出以腾出空间。

非常感谢您!

最佳答案

是的,可以通过 collections.deque :

from collections import deque

lst = deque([], 100)

list.append 一样,deque.append 就地工作:

A = deque(range(10), maxlen=10)

print(A)

deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxlen=10)

A.append(10)

print(A)

deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], maxlen=10)

关于python - 连续替换数组或列表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48472710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com