【问题标题】:Index of every first true value in pythonpython中每个第一个真值的索引
【发布时间】:2019-03-11 13:37:34
【问题描述】:

我只想返回所有第一个 True 值及其位置的列表。这里“on”等于True,“off”等于False

t = [False, False, False, False, True, True, True, False, False,
     True, True, True, True, False, False, False, False, False,
     False, False, True, True, True, False]

答案应该是一个列表

[4, 9, 20]

我尝试了以下代码:

[i for i, x in enumerate(t) if x]

返回

[4, 5, 6, 9, 10, 11, 12, 20, 21, 22]

有办法吗?

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:
    ret = []
    prev = False
    for i, x in enumerate(t):
        if x and not prev:
            ret.append(i)
        prev =  x
    print(ret)
    

    【讨论】:

      【解决方案2】:
      t = [False, False, False, False, True, True, True, False, False,
           True, True, True, True, False, False, False, False, False,
           False, False, True, True, True, False]
      
      indexes = []
      
      counter = 0
      for i in range(0, len(t)):
          el = t[i]
          if counter == 0:
              if el:
                  indexes.append(i)
                  counter = i
          else:
              if (el == True) and (i - counter == 1):
                  indexes.append(i)
                  counter = i
              else:
                  break
      print(indexes)
      

      【讨论】:

        【解决方案3】:

        如果你对第三方模块没问题pandas

        import pandas as pd
        t = [False, False, False, False, True, True, True, False, False,
             True, True, True, True, False, False, False, False, False,
             False, False, True, True, True, False]
        s=pd.Series(t)
        s = s.loc[s==True][s.shift(+1) != s]
        

        另一个使用 defaultdict 的没有 pandas 的解决方案

        from collections import defaultdict
        d=defaultdict(list)
        prev=t[0]
        d[prev].append(0)
        for idx,element in enumerate(t[1:],1):
        
            if element!=prev:
                d[element].append(idx)
                prev=element
        print(d)
        

        输出:

        defaultdict(list, {False: [0, 7, 13,23], True: [4, 9, 20]})
        

        【讨论】:

          【解决方案4】:

          如果您想要单线:

          on_list = [i for i, x in enumerate(t) if x and not (i and t[i-1])]
          print(on_list)
          

          但是,在这种情况下,没有理由使用单行使其过于复杂而无法阅读。

          【讨论】:

            【解决方案5】:

            由于这个答案可以帮助像我这样的其他人,我从这里复制了它enter link description here

            # Python3 code to demonstrate 
            # finding first True value 
            # using next() and enumerate()
              
            # initializing list 
            test_list = [ 0, 0, 5, 6, 0]
              
            # printing original list
            print ("The original list is : " + str(test_list))
              
            # finding first True value
            # using next() and enumerate()
            res = next((i for i, j in enumerate(test_list) if j), None)
              
            # printing result
            print ("The values till first True value : " + str(res))
            

            【讨论】:

              【解决方案6】:

              你快到了:

              [i for i, x in enumerate(t) if x and x != t[i-1]]
              

              在好评后编辑:(我的意思是,它仍然可以是单行的)

              [i for i, x in enumerate(t) if x and (x != (t[i-1] if i > 0 else False))]
              

              【讨论】:

              • 请注意这一点,因为如果第一个值为真,i-1 将等于 -1。因此,t[-1] 将取输入列表的最后一个元素。
              • 啊,这就是它检测呈阳性的原因...谢谢!
              猜你喜欢
              • 2017-08-17
              • 1970-01-01
              • 1970-01-01
              • 2020-09-30
              • 1970-01-01
              • 1970-01-01
              • 2022-12-01
              • 2019-09-19
              • 1970-01-01
              相关资源
              最近更新 更多