我正在嘗試設置了一個Python數組的索引,但預計它不是演戲:設置一個Python數組的索引
theThing = []
theThing[0] = 0
'''Set theThing[0] to 0'''
這將產生以下錯誤:
Traceback (most recent call last):
File "prog.py", line 2, in <module>
theThing[0] = 0;
IndexError: list assignment index out of range
什麼是在Python中設置數組索引的正確語法?
這是ideone,用同樣的錯誤:http://ideone.com/0IV1Sc#view_edit_box – 2013-04-05 02:00:18
'theThing = []'創建一個空數組,所以索引0不存在。 – 2013-04-05 02:01:47
我與Python相比並不相似(來自JavaScript背景),所以我覺得這很令人驚訝。在JavaScript中,您可以簡單地執行'var theThing = new Array(); theThing [0] = 0;'將theThing'的第0個元素設置爲0. – 2013-04-05 02:01:54