trying to split number but list index out of range

Question:

the problem with this code is that it will come to a point, in line 9 (if a[c+1] != 0:), it will recall an index +3 that does not exist and it will give me the error "list index out of range".

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)
Asked By: mansto

||

Answers:

U might want to include a check function to check if a[c+2] > length of string a so that the error will not occur as currently

or a easier way is to use try and except which you can handle the error using except like if the index is out of range means you are at the final few numbers in your string

Answered By: ONG JIA YUAN

You can split the string using the python regex module re.

import re

pattern = (r'[1-9]0*')
a= '10010010010100511'

print(list(map(int, re.findall(pattern, a))))

Output

[100, 100, 100, 10, 100, 5, 1, 1]
Answered By: bn_ln

Here is how I would do it without additional libraries. Check for values not equal to 0 to start a group, use a while loop to add the 0’s to that group as long as they appear. In that process you need to check whether the indices are less than the length of the input list.
I added some more strings to check for unintended behaviour, but as far as I can tell, it works.

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, 'n')

for string in tests:
    print(string)
    func(string)

Output:

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]
Answered By: Rabinzel
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.