[Python] parts of lists in Python, troubles with indexes

Discussão em 'Python' iniciado por Stack, Maio 17, 2025.

  1. Stack

    Stack Membro Participativo

    I'm fairly new to Python and the lists and indexing is giving me some trouble.

    What I want to do is:

    Get two individuals that represent a RPN expression, individual 1 and individual 2 for example:

    i1=['3', 'x', '*', '3' ,'4' ,'*', '/']
    i2=['x', '4', '/', 'x' ,'4' ,'+', '-']


    then, get a subset, of them and swap the subset, like so:

    from i1 I'd take subset [ '3' ,'4' ,'*'] and from i2 ['4'] (the final 3) then do a crossover producing two outputs:

    out1=['3', 'x', '*','4' , '/']
    out2=['x', '3' ,'4' ,'*', '/', 'x' ,'6' ,'+', '-']


    the code I've made is the following, and it keeps giving me erroneous results, like off by one errors, I don't want to go into the for cycle route, as I bet there is a more pythonesque way to do so. Can any one give me an hand please?

    def crossover(individual1,individual2,treedepth):
    from commonfunctions import getDepth,traverse,get_random_operator
    from commonfunctions import isOperator
    from generators import generate_RPN_expr
    #simple element mutation
    cxposindividual1 = random.randint(0, len(individual1) - 1)
    cxposindividual2 = random.randint(0, len(individual2) - 1)

    subtree1=traverse(individual1,cxposindividual1)
    subtree2 = traverse(individual2, cxposindividual2)
    individual1depth=getDepth(individual1)
    individual2depth = getDepth(individual2)
    subtree1depth=getDepth(subtree1)
    subtree2depth = getDepth(subtree2)


    output1=list()
    output1[:]=[]

    output2=list()
    output2[:]=[]


    #todo debug this !!!!

    #verificar se ecsolhemos um operador ou um nó terminal
    output1 = individual1[:len(individual1)+1-cxposindividual1-len(subtree1)]+subtree2+individual1[len(individual1)-cxposindividual1+1:]
    output2 = individual2[:len(individual2) + 1 - cxposindividual2 - len(subtree2)] + subtree1 + individual2[len(individual2) - cxposindividual2 + 1:]

    if len(output1) == 2 or len(output2) == 2:
    print('argh>>>') # problema !!!!

    #print ('CX')
    return (output1,output2)


    The traverse function returns the subtree in the selected position. getDepth isn't being used as yet.

    Thanks


    New Code


    def crossover(individual1,individual2,treedepth):
    from commonfunctions import getDepth,traverse,get_random_operator,traverse_with_indexes
    from commonfunctions import isOperator

    r1 = random.randrange(len(individual1))
    r2 = random.randrange(len(individual2))

    st1 = traverse(individual1, r1)
    st2 = traverse(individual2, r2)

    slice1 = slice(r1, r1+len(st1))
    slice2 = slice(r2, r2+len(st2))


    i1,i2 = individual1[:],individual2[:]
    a,b=i1[slice1],i2[slice2]
    i1[slice1],i2[slice2] = i2[slice2],i1[slice1]

    return i1, i2


    Here ,individual 1 equals ['3.8786681846845', 'x', '+'] and st1 = ['x'] the slice is 2,3 where I suppose it should be 1,2 but ... where individual 2 = ['x'] st2 = ['x'] the slice is 0,1 which is fine!

    I'm tempted to make some if blocks with the sizesfor exceptions, but I don't like exceptions

    Thanks

    Continue reading...

Compartilhe esta Página