python列表问题
时间:2011-05-27
来源:互联网
import random
existN = random.randint(1,100)
popN = random.randint(1,100)
exist=[existN]
pop=[popN]
i1=0
i2=0
i3=0
i4=0
while i1<existN: #exsit N1 numbers
n = random.randint(0,2**31-1)
exist[i1]=n
i1+=1
while i2<popN: #pop N2 numbers
n = random.choice(exist)
pop[i2]=n
i2+=1
while i3<popN: #sort N2 numbers
j = i3+1
while j<=popN:
if pop[i3]>pop[j]:
temp = pop[i3]
pop[i3]= pop[j]
pop[j]= temp
j+=1
i3+=1
while i4<popN: #print sort numbers
print pop[i4]
运行上面这段代码 报错如下:
Traceback (most recent call last):
File "Z:\pythonmyself\random.py", line 12, in <module>
exist[i1]=n
IndexError: list assignment index out of range
我没搞明白,索引怎么就越界了?
请高手指点!!!!!
existN = random.randint(1,100)
popN = random.randint(1,100)
exist=[existN]
pop=[popN]
i1=0
i2=0
i3=0
i4=0
while i1<existN: #exsit N1 numbers
n = random.randint(0,2**31-1)
exist[i1]=n
i1+=1
while i2<popN: #pop N2 numbers
n = random.choice(exist)
pop[i2]=n
i2+=1
while i3<popN: #sort N2 numbers
j = i3+1
while j<=popN:
if pop[i3]>pop[j]:
temp = pop[i3]
pop[i3]= pop[j]
pop[j]= temp
j+=1
i3+=1
while i4<popN: #print sort numbers
print pop[i4]
运行上面这段代码 报错如下:
Traceback (most recent call last):
File "Z:\pythonmyself\random.py", line 12, in <module>
exist[i1]=n
IndexError: list assignment index out of range
我没搞明白,索引怎么就越界了?
请高手指点!!!!!
作者: se865218660 发布时间: 2011-05-27
1. 重贴代码,缩进一塌糊涂
2. 自己debug下,作为任何软件开发者,这种程度的debug能力一定要有的。自己先记下来你期待的数组内容,和索引,然后把数组实际内容和索引打印出来,看是哪个不对。如果是数组不对,就检查和跟踪下数组相关的代码;索引不对也一样。
2. 自己debug下,作为任何软件开发者,这种程度的debug能力一定要有的。自己先记下来你期待的数组内容,和索引,然后把数组实际内容和索引打印出来,看是哪个不对。如果是数组不对,就检查和跟踪下数组相关的代码;索引不对也一样。
作者: iambic 发布时间: 2011-05-27
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28