【问题标题】:trying to split number but list index out of range试图拆分数字但列表索引超出范围
【发布时间】:2022-11-01 12:15:25
【问题描述】:

这段代码的问题是它会到达一个点,在第 9 行(如果 a[c+1] != 0:),它会调用一个不存在的索引 +3,它会给我错误“列表索引超出范围”。

a= '555101'
a= list(map(int,a)) 
c= 0
seq= []
    
for i in a:
    if a[c] == 1:
            
        if a[c+1] != 0:
            seq.append(i)
            c += 1
        elif a[c+3] == 0: #error
            if a[c+2] == 0:
                seq.append(1000)
                c += 1
            elif a[c+2] != 0:
                seq.append(10)
                c += 1
        elif a[c+2] == 0:
            if a[c+1] == 0:
                seq.append(100)
                c += 1
            elif a[c+1] != 0:
                seq.append(1)
                c += 1
    elif a[c] == 0:
        c += 1
    elif a[c] == 5:
        seq.append(i)
        c += 1
print(seq)

【问题讨论】:

    标签: python list indexing range


    【解决方案1】:

    您可以使用 python regex module re 拆分字符串。

    import re
    
    pattern = (r'[1-9]0*')
    a= '10010010010100511'
    
    print(list(map(int, re.findall(pattern, a))))
    

    输出

    [100, 100, 100, 10, 100, 5, 1, 1]
    

    【讨论】:

      【解决方案2】:

      这是我在没有额外库的情况下如何做到的。检查不等于 0 的值以启动一个组,只要它们出现,就使用 while 循环将 0 添加到该组。在该过程中,您需要检查索引是否小于输入列表的长度。 我添加了更多字符串来检查意外行为,但据我所知,它有效。

      tests = [
          '10010010010100511',
          '00010010010100511',
          '10010010010100510',
          '10010051110010100',
      ]
      
      def func(string):
          a = list(map(int,string))
          all_groups = []
          group = []
          i = 0
          while i < len(a):
              if a[i] != 0:
                      group.append(a[i])
                      j = i + 1
                      while (j < len(a)) and (a[j] == 0):
                          group.append(a[j])
                          j += 1
                      all_groups.append(group)
                      group = []
                      i = j
                      if group:
                          all_groups.append(group)
                          i += 1
              else:
                  i += 1
          out = [int(''.join([str(digit) for digit in elem])) for elem in all_groups]
          print(out, '
      ')
      
      for string in tests:
          print(string)
          func(string)
      

      输出:

      10010010010100511
      [100, 100, 100, 10, 100, 5, 1, 1] 
      
      00010010010100511
      [100, 100, 10, 100, 5, 1, 1]
      
      10010010010100510
      [100, 100, 100, 10, 100, 5, 10]
      
      10010051110010100
      [100, 100, 5, 1, 1, 100, 10, 100]
      

      【讨论】:

      • @mansto 发生了什么变化?
      【解决方案3】:

      您可能希望包含一个检查函数来检查 a[c+2] 是否大于字符串 a 的长度,以便当前不会发生错误。

      或者更简单的方法是使用 try 和 except ,您可以使用 except 来处理错误,如果索引超出范围意味着您位于字符串中的最后几个数字

      【讨论】:

      • 我认为 c+x &gt; length of string 与 x 是 2、3、0 或 st(你写 a[c+2])
      猜你喜欢
      • 1970-01-01
      • 2017-10-24
      • 1970-01-01
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多