I just started learning Python and need help analyzing data in an excel spreadsheet. The excel sheet has i.e. 111122221112211 in one column and I’m trying to figure out how many changes from 1 to 2 or 2 to 1 occur here. In this case, 4 times.
I’ve been told to use set.difference but I don’t understand how. So I tried my own way and get the error ‘list index out of range’ at the elif part. Here is my script (I’m still working on the basics so this may seem very amateur):
a = xlws.Columns(5).value
a = [list(x)[0] for x in a[1:400]]
beam = 0
for x in range(len(a)):
if a[x] ==1 and a[x+1]==2:
beam += 1
elif a[x]==2 and a[x+1]==1:
beam += 1
else:
beam = beam
The line
for x in range(len(a)):should bebecause you are getting a list element with index
x+1inside the loop and therefore you are reading one element too much.