微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

倒数

如何解决倒数

| 我有一个这样组织的清单:
[(\'down\',0.0098000000000000309),(\'up\',0.0015000000000000568),(\'down\',0.008900000000000019),0.023300000000000098),0.011599999999999944),0.0027000000000000357),0.0023999999999999577),0.0065000000000000613),0.0057000000000000384),0.018400000000000083),0.009300000000000086),0.0038000000000000256),0.00050000000000005596),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
    ,
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 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
    ,我的建议(尽管现在有了第三部分,这已经不再是那么漂亮了):
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:]))
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。