У меня есть список, организованный следующим образом:
[('down', 0.0098000000000000309),
('up', 0.0015000000000000568),
('down', 0.008900000000000019),
('down', 0.023300000000000098),
('down', 0.011599999999999944),
('down', 0.0027000000000000357),
('up', 0.0023999999999999577),
('up', 0.0065000000000000613),
('down', 0.0057000000000000384),
('down', 0.018400000000000083),
('up', 0.009300000000000086),
('down', 0.0038000000000000256),
('down', 0.00050000000000005596),
('up', 0.0082000000000000961), .....
Какой был бы лучший способ "сравнить назад?", в основном я хочу вернуть "да" (или что-то еще). ЕСЛИ у нас есть серия из 2 "спадов", за которой следует одно "вверх", а второе значение ниже 0,0095.
Надеюсь, он имеет смысл.
Здесь вы идете:
def frob(l):
downcount = 0
for ele in l:
if downcount >= 2 and ele[0] == 'up' and ele[1] < 0.0095:
return True
downcount = (downcount + 1) if ele[0] == 'down' else 0
return False
Создайте скользящее окно и проверьте на нем:
def slidingwindow(iterable):
iterator = iter(iterable)
first, second = iterator.next(), iterator.next()
for next in iterator:
yield (first, second, next)
first, second = second, next
def testforcondition(data):
for window in slidingwindow(data):
direction = [w[0] for w in window]
if direction == ['down', 'down', 'up'] and window[2][1] < 0.0095:
return True
return False
Здесь моя попытка:
def test(data):
for x in xrange(2, len(data)):
if data[x-2][0] is 'down' and data[x][x-1] is 'down' and data[x][0] is 'up' and data[x][1] < 0.0095:
return True
return False
for index in xrange(0, len(list) - 2):
if list[index][0] == 'down' and list[index + 1][0] == 'down' and list[index + 2][0] == 'up' and list[index + 1][1] < 0.0095:
return True
Мое предложение (хотя теперь с третьим фрагментом, это уже не очень красиво):
def compback(l):
return any(i1[0] == i2[0] == "down"
and i2[1] < 0.0095
and i3[0] == "up"
for i1, i2, i3 in zip(l, l[1:], l[2:]))