mar*_*995 0 python arrays variable-length-array
testList= []
testList[12]= 31
testList[23]= 1337
Error: IndexError: list assignment index out of range
Run Code Online (Sandbox Code Playgroud)
基本上,我有唯一的Integer,并且我想将列表用于哈希函数h(x)= x(因为它们是唯一的)
我可以这样初始化长度:
testList= [0 for i in range(50)]
Run Code Online (Sandbox Code Playgroud)
但是然后我必须确定大小和我的唯一数字,随着时间的推移,这些数字和唯一数字会增加。可以将大小设置为例如1-2Mio,还是可以动态地执行此操作?Java中的ArrayList <>是动态用于附加和删除的,Python中的List也是如此。
谢谢!
也许您需要一个dict:
testList = {}
testList[12]= 31
testList[23]= 1337
print(testList)
print(testList[23])
Run Code Online (Sandbox Code Playgroud)
输出:
{12: 31, 23: 1337}
1337
Run Code Online (Sandbox Code Playgroud)