Search code examples
pythonarraysloopsfor-loopinteger

element-specific array on array function


[TypeError: unsupported operand type(s) for -: 'list' and 'int']

enter image description here

I want to create a new array by calling on elements in array1 and array2. However, I want the elements I call on in array1 to correspond to the array2, i.e. array[i] and brray[i] operating together and array [i+1] operating with brray [i+1] for example.

I'd like to iterate, but I'm using this new array for further operations


Solution

  • You need to zip() both the lists to fetch corresponding elements in parallel:

    from math import cos, pi
    array = [1,2,3,4,5]
    brray = [6,7,8,9,10]
    new=[]
    
    for i,j in zip(array,brray):
        if 1<i<3:
            new.append((j-12)/(cos((2*pi/10)*(i-2))))
        elif 3<=i<6:
            new.append((j-12)/(cos((2*pi/10)*(i-10))))
    
    print(new)
    #output
    [-5.0, 12.944271909999152, 3.708203932499369, 2.0]