在Python上列出索引超出范围。什么都不管用
我已经复习了多个对我的问题有类似答案的帖子。不管我怎么努力,似乎什么都不管用 我正在尝试创建100个随机数,并将这些随机数放入列表中。然而,我不断地在Python上列出索引超出范围。什么都不管用,python,Python,我已经复习了多个对我的问题有类似答案的帖子。不管我怎么努力,似乎什么都不管用 我正在尝试创建100个随机数,并将这些随机数放入列表中。然而,我不断地 File "E:\WorkingWithFiles\funstuff.py", line 17, in randNumbs numbList[index]+=1 IndexError: list index out of range 我的代码是: def randNumbs(numbCount): numbList=[0]*100
File "E:\WorkingWithFiles\funstuff.py", line 17, in randNumbs
numbList[index]+=1
IndexError: list index out of range
我的代码是:
def randNumbs(numbCount):
numbList=[0]*100
i=1
while i < 100:
index = random.randint(1,100)
numbList[index]+=1
i+=1
print (numbList)
return (numbList)
等等。我无法生成随机数。如果有人知道我的列表索引超出范围的原因以及如何提供帮助,我们将不胜感激!谢谢大家!
编辑:该.txt文件长113k个单词您在此处列出了大小为100的列表:
numbList=[0]*100
您的问题是,当您应该访问索引0-99时,您创建了从1到100的索引。给定一个大小为n
的列表,有效的列表索引为0
到n-1
将代码更改为
index = random.randint(0,99)
看起来像是一个一个接一个的错误。randint将返回数字1到100,而列表的索引为0到99 此外,您还可以像这样重写代码:
def randNumbs(numbCount):
return [random.randint(1, 100) for i in range(numbCount)]
我会以不同的方式处理这个问题:
from random import sample
SAMPLE_SIZE = 100
# load words
with open("dictionary.txt") as inf:
words = inf.read().splitlines() # assumes one word per line
# pick word indices
# Note: this returns only unique indices,
# ie a given word will not be returned twice
num_words = len(words)
which_words = sample(range(num_words), SAMPLE_SIZE)
# Note: if you did not need the word indices, you could just call
# which_words = sample(words, SAMPLE_SIZE)
# and get back a list of 100 words directly
# if you want words in sorted order
which_words.sort()
# display selected words
print("Number Word")
for w in which_words:
print("{:6d} {}".format(w, words[w]))
这就产生了
Number Word
198 abjuring
2072 agitates
2564 alevin
6345 atrophies
8108 barrage
9155 begloom
10237 biffy
11078 bleedings
11970 booed
14131 burials
14531 cabal
# etc...
在这里,我试图修复你的代码。评论中的解释
import random
def rand_numbs(numb_count):
# this will generate a list of length 100
# it will have indexes from 0 to 99
numbList = [0] * 100
# dont use a while loop...
# when a for loop will do
for _ in range(numb_count):
# randint(i, j) will generate a number
# between i and j both inclusive!
# which means that both i and j can be generated
index = random.randint(0, 99)
# remember that python lists are 0-indexed
# the first element is nlist[0]
# and the last element is nlist[99]
numbList[index] += 1
print (numbList)
return (numbList)
你的重写与OP的代码不一样。我运行了这段代码,它使我的计算机崩溃了。我可能应该提到.txt文件是113k字长的,它不会使你的计算机崩溃!不应提交任何申请。您使用的是哪个操作系统,是如何崩溃的?还是说它使您的进程崩溃了?
i
没有被用作列表索引。这是被用作索引的randint
的结果。我试图修复它,但我取得了部分成功,直到它只开始工作了一半时间。113809 [0, 0, 1, 2, 1, 0, 2, 1, 0, 2, 0, 0, 2, 1, 2, 1, 0, 0, 1, 1, 0, 1, 2, 1, 2, 1, 0, 4, 2, 1, 1, 0, 0, 0, 2, 0, 1, 1, 2, 0, 1, 0, 0, 0, 2, 2, 0, 0, 0, 1, 0, 2, 0, 1, 1, 2, 2, 0, 0, 2, 2, 2, 1, 1, 2, 0, 2, 0, 3, 3, 2, 1, 1, 2, 0, 0, 0, 0, 3, 0, 2, 1, 1, 1, 0, 1, 0, 1, 0, 1, 2, 0, 0, 2, 1, 3, 1, 1, 2, 1]然后我会得到>>>================================================================113809回溯(最后一次调用):randNumbs(numbCount)文件“E:\WorkingWithFiles\funstuff.py”第26行的文件“E:\WorkingWithFiles\funstuff.py”,randNumbs numbList[索引]第17行+=1索引器错误:列表索引超出范围Python使用基于0的索引。包含100个元素的列表的索引为0-99。
import random
def rand_numbs(numb_count):
# this will generate a list of length 100
# it will have indexes from 0 to 99
numbList = [0] * 100
# dont use a while loop...
# when a for loop will do
for _ in range(numb_count):
# randint(i, j) will generate a number
# between i and j both inclusive!
# which means that both i and j can be generated
index = random.randint(0, 99)
# remember that python lists are 0-indexed
# the first element is nlist[0]
# and the last element is nlist[99]
numbList[index] += 1
print (numbList)
return (numbList)