Search code examples
pythonenumerated-types

How can I complete match between arrays in arrays by using enumerate?


I have 2 array,

x_test=[(2 1 3 2 5 1),(2 1 3 4 1 2).........] # len(x_test)=172
y_test=[2,1,3,4,5,1,3,5.....................] # len(y_test)=172

How can I match them by index like (2 1 3 2 5 1) 2 ;(2 1 3 4 1 2) 1.

print(list(enumerate(x_test,y_test)))

error:

'list' object cannot be interpreted as an integer
print(list(enumerate(x_test,y_test)))
print(list(enumerate(x_test,y_test)))

error:

TypeError                                 Traceback (most recent call last)
<ipython-input-48-491946acd941> in <module>
----> 1 print(list(enumerate(x_test,y_test)))

TypeError: 'list' object cannot be interpreted as an integer

Solution

  • You should use zip:

    x_test=[(2,1,3,2,5,1),(2,1,3,4,1,2)]
    y_test=[1,2]
    
    for a, b in zip(x_test, y_test):
        print (a,b)
    
    #(2, 1, 3, 2, 5, 1) 1
    #(2, 1, 3, 4, 1, 2) 2