这可能吗:
myList = []
myList[12] = 'a'
myList[22] = 'b'
myList[32] = 'c'
myList[42] = 'd'
当我尝试时,我得到:
# IndexError: list assignment index out of range #
您必须预先填写一些内容(例如0
or None
),然后才能对其进行索引:
myList = [None] * 100 # Create list of 100 'None's
myList[12] = 'a' # etc.
或者,使用字典而不是列表,如亚历克斯·马尔泰利建议 https://stackoverflow.com/questions/869778/populating-a-list-array-by-index-in-python/869805#869805.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)