[Solved] In Python how can I change the values in a list to meet certain criteria


Assuming the existing values don’t matter this would work

def fixList(inputList, splitChar="X"):
    outputList = inputList[:]
    x = None 
    for i in xrange(len(outputList)):
        if outputList[i] == splitChar:
            outputList[i] = x = 0 
        elif x is None:
            continue 
        else:
            outputList[i] = x 
            x += 1
    return outputList

eg

>>> a = ['X',1,2,3,4,5,6,7,8,9,'X',11,12,13,14,15,16,17,18,19,20]
>>> fixList(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> b = ['y',1,2,3,4,5,6,7,8,9,10,'y',12,13,14,15,16,17,18,19,20]
>>> fixList(b, splitChar="y")
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

EDIT: fixed to account for the instances where list does not start with either X or 0,1,2,…

2

solved In Python how can I change the values in a list to meet certain criteria