Problem with a loop in Python. Below below there's a 'handwritten' code - which works and it works in a way it's supposed to work. I explain:
new[i]
is a list where each element is a time of a blink.df2[' time'][i]
anddf2[' time'][i+1]
designate a time range fornew[i]
; if the time ofnew[i]
is between those twodf2s...[i],[i+1]
, thenprint(df2['letter'][i])
- else try with a higher index until you find the right range, then print a letter with that higher index.
Ok so it works. But when I try to write an algorithm using WHILE .. AND or WHILE NOT .. AND, (tried a few versions...) e.g.:
(this doesn't work:)
for j in range(len(new)):
while not new[j]>df2[' time'][i] and new[j]<df2[' time'][i+1]:
i+1
prin开发者_运维问答t(df2['letter'][i])
, which I understand as:
- for each element in list
new[]
, as long as this element isn't bigger thandf2[' time'][i]
and isn't smaller then timedf2[' time'][i+1]
, increase iteration by one, unless you found the right match, then print df2['letter'][i].
My problem is that i
is always 1
, no matter what. Basically: what's wrong in my algorithm?
(this works:)
if new[0]>df2[' time'][0] and new[0]<df2[' time'][1]:
print(df2['letter'][0])
elif new[0]>df2[' time'][1] and new[0]<df2[' time'][2]:
print(df2['letter'][1])
if new[1]>df2[' time'][2] and new[1]<df2[' time'][3]:
print(df2['letter'][2])
elif new[1]>df2[' time'][3] and new[1]<df2[' time'][4]:
print(df2['letter'][3])
if new[2]>df2[' time'][4] and new[2]<df2[' time'][5]:
print(df2['letter'][4])
elif new[2]>df2[' time'][5] and new[2]<df2[' time'][6]:
print(df2['letter'][5])
elif new[2]>df2[' time'][6] and new[2]<df2[' time'][7]:
print(df2['letter'][6])
elif new[2]>df2[' time'][7] and new[2]<df2[' time'][8]:
print(df2['letter'][7])
elif new[2]>df2[' time'][8] and new[2]<df2[' time'][9]:
print(df2['letter'][8])
elif new[2]>df2[' time'][9] and new[2]<df2[' time'][10]:
print(df2['letter'][9])
elif new[2]>df2[' time'][10] and new[2]<df2[' time'][11]:
print(df2['letter'][10])
elif new[2]>df2[' time'][11] and new[2]<df2[' time'][12]:
print(df2['letter'][11])
I tried while not
loop with conjunction. Increase iteration by 1 seems not working.
You aren't actually updating i here. You need to state i=i+1
精彩评论