简体   繁体   中英

How do you find the index of an element in an Array on Python while in loop

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!

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

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]

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

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]
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM