使用for for for for for for python索引列表
对于python中的列表索引,我知道该索引从0开始。例如,在我创建的列表列表中:
lst = [['Youngstown', ['OH', 4110, 8065, 115436]], ['Yankton', ['SD', 4288, 9739, 12011, [966]]], ['Yakima', ['WA', 4660, 12051, 49826, [1513, 2410]]]...]
lst[2]: ['Yakima', ['WA', 4660, 12051, 49826, [1513, 2410]]] #would be index 2
如果我试图找到[1513,2410],则使用以下内容:
lst[2][1][4]: [1513, 2410]
所以我是试图将一个数字与以下索引范围内的所有数字进行比较:lst [i] [1] [4]其中i = 0,
所以我认为我可以使用一定的循环来执行此操作,但它一直说它超出了范围。
a = 0
l = []
while(a < len(lst)):
i = 0
for j in range(len(lst[i][1][4])):
if 1000 < j:
l.append(lst[a])
i += 1
a += 1
这是我收到的错误:
Trackback(最近的电话最后一次): 文件“&lt; pyshell#38&gt;”,第3行,in 对于J的J(LEN(LST [i] [1] [4])): indexError:列表索引以外
For a list index in python, I understand that the index starts at 0. For example in my list of list that I have created:
lst = [['Youngstown', ['OH', 4110, 8065, 115436]], ['Yankton', ['SD', 4288, 9739, 12011, [966]]], ['Yakima', ['WA', 4660, 12051, 49826, [1513, 2410]]]...]
lst[2]: ['Yakima', ['WA', 4660, 12051, 49826, [1513, 2410]]] #would be index 2
And the following would be the used if I was trying to find [1513, 2410]:
lst[2][1][4]: [1513, 2410]
So I was trying to compare a number to all the numbers in the following index range: lst[i][1][4] where i = 0
So I thought I could use a while loop to do this but it keeps saying it is out of range.
a = 0
l = []
while(a < len(lst)):
i = 0
for j in range(len(lst[i][1][4])):
if 1000 < j:
l.append(lst[a])
i += 1
a += 1
Here is the error I receive:
Traceback (most recent call last):
File "<pyshell#38>", line 3, in
for j in range(len(lst[i][1][4])):
IndexError: list index out of range
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一次通过
循环,
i
是0,因此您正在尝试访问lst [0] [1] [1] [4]
。lst [0] [1]
是['oh',4110,8065,115436]
。它有4个元素,因此索引[4]
(第五元素)超出范围。The first time through the
while
loop,i
is 0, so you're trying to accesslst[0][1][4]
.lst[0][1]
is['OH', 4110, 8065, 115436]
. It has 4 elements, so index[4]
(the fifth element) is out of range.