如何在Python while in循环中找到数组中元素的索引

发布于 2024-11-05 19:12:59 字数 332 浏览 1 评论 0原文

抱歉,但我对编程相当陌生,似乎找不到与我需要的内容相关的任何内容...

while i<size(Array):
      for index, k in enumerate(Array1):
             if (k==Array[i]):
                   print index
                   i=i+1

上面的代码提供了一个索引超出范围的输出... 请注意,Array1 的元素少于 Array。

我想知道如何在没有错误的情况下运行循环,并且我不太确定导致错误的原因。

谢谢你!

Sorry but I'm fairly new to programming and cannot seem to find anything that relates to what I need...

while i<size(Array):
      for index, k in enumerate(Array1):
             if (k==Array[i]):
                   print index
                   i=i+1

The above code presents an output that has the index out of bounds...
Note that Array1 has less elements then Array.

I'm wondering how it would be possible to run the loop without the error and I'm not really sure what is causing the error.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

完美的未来在梦里 2024-11-12 19:12:59

我是一个索引,你更新它太频繁了。移动 i = i+1 的缩进以消除索引越界。

while i<size(Array):
      for index, k in enumerate(Array1):
             if (k==Array[i]):
                   print index
      i=i+1

i is an index and you are updating it too often. Move the indentation of i = i+1 to get rid of the index out of bounds.

while i<size(Array):
      for index, k in enumerate(Array1):
             if (k==Array[i]):
                   print index
      i=i+1
哽咽笑 2024-11-12 19:12:59

如果您只想要第一个索引(或者只有一个索引),那么您可以使用索引函数:

for i in array2:
    if i in array1:
        print array1.index(i)

要获取索引列表:

print [array2.index(i) for i in array1 if i in array2]

If you only want the first index (or there will only be one index) then you can use the index function:

for i in array2:
    if i in array1:
        print array1.index(i)

To get a list of the indices:

print [array2.index(i) for i in array1 if i in array2]
还如梦归 2024-11-12 19:12:59

符来测试某个值是否在列表中:

Array = [1,2,3,4,5,6,7,8]
Array1 = [2,9,3,9,1,9,2]

for i, value in enumerate(Array1):
    if value in Array:
        print i

您可以使用 in 成员资格测试运算

0
2
4
6

You can test whether a value is in a list by using the in membership testing operator:

Array = [1,2,3,4,5,6,7,8]
Array1 = [2,9,3,9,1,9,2]

for i, value in enumerate(Array1):
    if value in Array:
        print i

output

0
2
4
6
梓梦 2024-11-12 19:12:59

看起来好像您想打印内部数组中的所有索引,其中值与输出数组中的值匹配:

array1 = [1,2,3,4,5,6,7,8]
array2 = [2,9,3,9,1,9,2]
for i,a1 in enumerate(array1):
    for j,a2 in enumerate(array2):
        if a1 == a2:
            print j

输出:

4
0
6
2

如果您想收集索引,则需要列表理解:

print [j for i,a1 in enumerate(array1) for j,a2 in enumerate(array2) if a1 == a2]

输出:

[4, 0, 6, 2]

或者可能按排序顺序:

print sorted(j for i,a1 in enumerate(array1) for j,a2 in enumerate(array2) if a1 == a2)

输出:

[0, 2, 4, 6]

It looks as if you want to print all indexes in the inner array where the value matches a value in the output array:

array1 = [1,2,3,4,5,6,7,8]
array2 = [2,9,3,9,1,9,2]
for i,a1 in enumerate(array1):
    for j,a2 in enumerate(array2):
        if a1 == a2:
            print j

Output:

4
0
6
2

If you want to collect the indices, you a list comprehension:

print [j for i,a1 in enumerate(array1) for j,a2 in enumerate(array2) if a1 == a2]

Output:

[4, 0, 6, 2]

Or maybe in sorted order:

print sorted(j for i,a1 in enumerate(array1) for j,a2 in enumerate(array2) if a1 == a2)

Output:

[0, 2, 4, 6]
囍孤女 2024-11-12 19:12:59
while i<size(Array):
      for index, k in enumerate(Array1):
             if (k==Array[i]):
                   print index
                   i=i+1

如果我理解正确,您认为 i 不应该超过数组的大小,因为您有一段时间 i i i i i i i i i i i i i size(Array) 但 while 条件仅控制循环是否重复,并不能保证整个循环中 i 始终小于 size。

您似乎认为这将在数组上循环一次,但因为一个循环位于另一个循环内部:循环本身将重复。

在这种情况下,i 在内部循环中为 Array1 中的每个元素递增一次,这导致它对于索引 Array 来说变得太高。

我认为你想要的是同时迭代两个列表。不要通过创建两个循环来实现这一点,您只需要一个循环。

for index, k in enumerate(Array):
     if k == Array1[index]:
          print index

更好的方法,但对于初学者来说可能更难理解是:

for index, (value1, value2) in enumerate(zip(Array, Array1)):
   if value1 == value2:
        print index

Zip 将两个列表“压缩”在一起,这使得并行迭代两个列表变得非常容易。

while i<size(Array):
      for index, k in enumerate(Array1):
             if (k==Array[i]):
                   print index
                   i=i+1

If I understand correctly, you are thinking that i shouldn't go above the size of Array, because you have a while i < size(Array) But the while condition only controls whether the loop will repeat, it does not guarantee that i will remain less then size throughout the loop.

You seem to be thinking that this will loop over the arrays once, but because one loop is inside the other: the loop itself will be repeated.

In this case, i is incremented inside the inner loop once for every element in Array1, which causes it to become too high for indexing Array.

I think what you want is to iterate over both lists at the same time. Don't do this by creating two loops, you only want one loop.

for index, k in enumerate(Array):
     if k == Array1[index]:
          print index

A better approach, but perhaps harder to understand for a beginner is this:

for index, (value1, value2) in enumerate(zip(Array, Array1)):
   if value1 == value2:
        print index

Zip "zips" the two lists together which makes it really easy to iterate over both in parallel.

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