使用for for for for for for python索引列表

发布于 2025-01-25 11:56:56 字数 812 浏览 1 评论 0原文

对于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-小熊_ 2025-02-01 11:56:56

第一次通过循环,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 access lst[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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文