#随机选取一个真实用户
userIndex = random.randint(0,len(allUserLocation))
realUserLocation = allUserLocation[userIndex]
print(len(allUserLocation))
del allUserLocation[userIndex]
print(len(allUserLocation))
Traceback (most recent call last):
File "C:/Users/Zhang Liqiang/Desktop/lbs/lbs_algorithm/lbs_algorithm/test_algorithm/showAllUser_geohash.py", line 17, in
realUserLocation = allUserLocation[userIndex]
IndexError: list index out of range
改成
userIndex = random.randint(0,len(allUserLocation)-1)
比如你有4个user , 下标范围为 0-3
random.randint(0,4) 有可能取到 4
而 4 下标是不存在的。
因为例如random.randint(0,3)生成的随机数为0,1,2,3,当len(allUserLocation)恰好被生成时,allUserLocation[userIndex]会越界,数组的下标是从0开始计数的