python create list with length
You cannot assign to a list like lst[i] = something , unless the list already is initialized with at least i+1 elements. You need to use append to add elements to the ... , Perhaps you need a dict : testList = } testList[12]= 31 testList[23]= 1337 print(testList) print(testList[23]). Output: 12: 31, 23: 1337} 1337.,(tl;dr: The exact answer to your question is numpy.empty or numpy.empty_like , but you likely don't care and can get away with using myList = [None]*10000 .) ... , So make a list that contains 10 zeros or something to be sure that my list has a specific length. share.
相關軟體 Python 資訊 | |
---|---|
![]() python create list with length 相關參考資料
Create an empty list in python with certain size - Stack Overflow
You cannot assign to a list like lst[i] = something , unless the list already is initialized with at least i+1 elements. You need to use append to add elements to the ... https://stackoverflow.com Create List with variable length in Python - Stack Overflow
Perhaps you need a dict : testList = } testList[12]= 31 testList[23]= 1337 print(testList) print(testList[23]). Output: 12: 31, 23: 1337} 1337. https://stackoverflow.com How to create a fix size list in python? - Stack Overflow
(tl;dr: The exact answer to your question is numpy.empty or numpy.empty_like , but you likely don't care and can get away with using myList = [None]*10000 .) ... https://stackoverflow.com Initialise a list to a specific length in Python - Stack Overflow
So make a list that contains 10 zeros or something to be sure that my list has a specific length. share. https://stackoverflow.com |