在Python中自动增长列表

在Python中自动增长列表,第1张

在Python中自动增长列表

当然有可能,您只需要使用list的子类即可

class GrowingList(list):    def __setitem__(self, index, value):        if index >= len(self): self.extend([None]*(index + 1 - len(self)))        list.__setitem__(self, index, value)

用法

>>> grow = GrowingList()>>> grow[10] = 4>>> len(grow)11>>> grow[None, None, None, None, None, None, None, None, None, None, 4]


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5644550.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存