【问题标题】:how to check a list without the error:list index out of range [duplicate]如何在没有错误的情况下检查列表:列表索引超出范围[重复]
【发布时间】:2018-10-05 01:46:34
【问题描述】:

我有一个这样的索引列表:

index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]

我得到一个:

pattern_width = 4
n = pattern_width
final_list = []

我将如何遍历一次分析 n 个元素的列表,如果存在所有元素的值相等的情况,它们将被附加到一个空列表中?

因此,由于前 4 个元素是 [5, 5, 5, 5],因此值 5 将附加到 final_list。但是,由于接下来的 4 个元素是 [5, 5, 5, 6],因此不会附加。

解决方案是 [5, 16, 34, 42]

我一直遇到的问题是list index out of range

我的做法是:

for i in range(len(index_max)):
    x = index_max[i]==index_max[i+(pattern_width)
    final_list.append(x)

但是,这在列表末尾不起作用。我将如何解决这个问题?谢谢。

【问题讨论】:

  • 列表总是排序的吗?

标签: python python-3.x list indexing range


【解决方案1】:
index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]   
n = 4
final_list = [index_max[i] for  i in range(len(index_max)-(n-1)) if len(set(index_max[i:i+n])) == 1]

这应该可以解决问题。打破它,

  1. index_max[i] 满足条件时返回索引值。
  2. range(len(index_max)-(n-1)) - 这将搜索列表中 4 的每个组合。 n-1 将确保列表在长度为 4 的最后一个组合处停止。
  3. len(set(index_max[i:i+n])) == 1 将测试列表转换为一个集合。这将允许您根据集合的长度评估唯一值。

如果您担心列表中的重复值,您只需使用如下所示的集合推导,

final_list = {index_max[i] for  i in range(len(index_max)-(n-1)) if len(set(index_max[i:i+n])) == 1}

【讨论】:

    【解决方案2】:

    这是一个使用来自 itertools 的一堆方法的解决方案。我继续将问题分成 3 个部分,这样如果您需要使用代码做其他事情,那么它会更加灵活。

    import itertools
    index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]
    n = 4
    
    def all_equal(iterable):
        g = itertools.groupby(iterable)
        return next(g, True) and not next(g, False)
    
    def window(iterable, window_size):
        iters = itertools.tee(iterable, window_size)
        for i in range(1, window_size):
            for it in iters[i:]:
                next(it, None)
        return zip(*iters)
    
    def uniques_from_window(iterable, window_size):
        return [sub_list[0] for sub_list in window(iterable, window_size) if all_equal(sub_list)]
    
    print(uniques_from_window(index_max, n))
    

    输出

    [5, 16, 34, 42]
    

    【讨论】:

      【解决方案3】:

      如果列表总是排序的,只需count每个值:

      final_list = [candidate for candidate in set(index_max) if index_max.count(candidate) >= pattern_width]
      

      由于大多数调用都是对内置函数的,所以这应该足够快。

      如果您需要实际的窗口,请对您的方法进行两项修改:

      1. 仅迭代到结束前的位置pattern_width
      2. 使用set 来识别唯一的窗口。

        # stop iteration ``pattern_width`` elements before the end
        for i in range(len(index_max) - pattern_width):
          # slice the window from the list and eliminate duplicates
          window_elements = set(index_max[i:i+pattern_width])
          if len(window_elements) == 1:
            final_list.extend(window_elements)
        

      【讨论】:

        【解决方案4】:

        试试这个:

        index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]
        pattern_width = 4
        
        final_list = []
        
        
        for i in range(len(index_max)-3):
            count = 1
            for j in range(pattern_width):
                if index_max[i] == index_max[i+j]:
                    count += 1
            if count == 4:        
                final_list.append(index_max[i])
        print(final_list)
        

        【讨论】:

          【解决方案5】:

          你可以试试下面的

          index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]
          pattern_width = 4
          final_list = []
          
          for i in range(len(index_max) - pattern_width):
              temp = index_max[i:i + pattern_width]
              s = set(temp)
              if len(s) == 1:
                  final_list.append(temp[0])
          
          print(final_list) # Output [5, 16, 34, 42]
          

          工作示例https://ideone.com/Rz2gbK

          【讨论】:

            猜你喜欢
            • 2013-04-07
            • 1970-01-01
            • 1970-01-01
            • 2019-10-01
            • 1970-01-01
            • 2015-01-28
            • 1970-01-01
            • 2017-10-30
            相关资源
            最近更新 更多