1

[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

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
  • Does this answer your question? [How do I iterate through two lists in parallel?](https://stackoverflow.com/questions/1663807/how-do-i-iterate-through-two-lists-in-parallel) – SuperStormer May 23 '23 at 05:28
  • Welcome to SO! Please do not post images of your code. Copy paste your code as text. – Talha Tayyab May 23 '23 at 05:50
  • Please clarify your question by showing the required output. The TypeError is due to *(brray-12)* Also, did you really intend to ignore the first element of each of the lists? – DarkKnight May 23 '23 at 06:20

1 Answers1

0

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]
Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
  • Thank you very much, it worked for the arrays; for similar arrays- where one i imported from excel, I received an error (unsupported operand type(s) for /: 'NoneType' and 'float'). What would you think is wrong with it... (And sorry, I will be copying and pasting codes from now on!) – weezingweezer May 23 '23 at 06:28
  • `None Type` means there is no data coming up from excel. It means it is taking blank values when fetching value from excel. – Talha Tayyab May 23 '23 at 06:29
  • Thank you for the help with the code, and I didn't realise the tick was for that, thank you for that also. – weezingweezer May 23 '23 at 07:13