Artoria
Artoria

Reputation: 31

Python 3 program with nested list, prompted 'tuple' class error when compiling

Found the problem in the function HerusticSort, I'll check that.


I wrote a piece of python code using nested list like:

opentab = [[start], [0], [None]]

node = opentab[0].pop(0)

But when executing the code, it prompted

File "\Herustic Search.py", line 97, in HerusticSearchA

node = opentab[0].pop(0)

AttributeError: 'tuple' object has no attribute 'pop'

It's very weird because I didn't change it's class and I've already define it as nested list.


After the problem, I set an assertion to make sure it's right.

assert isinstance(opentab[0], list)

And of course now I got an AssertionError

I also tried in IDLE one by one, and no problem.

I just put complete code piece here, I don't know why this happened...


def HerusticSearchA(start, end):
    '''
    
    '''

    def H_Function(state, depth):
        '''
        state is a 3 x 3 list, stands for the puzzle state.
        returns the herustic assessment value
        g for the depth of the node, h for the incorrect tile number
        '''
        crit = copy.deepcopy(end)
        count = 0
        for i in range(len(state)):
            for j in range(len(state[0])):
                if state[i][j] - crit[i][j] != 0:
                    count += 1
        return depth + count


    #1. Variables
    #[[state], [value], [father]]
    opentab = [[start], [0], [None]]
    close = [[], [], []]

    depth = 0
    done = False

    print(start, end)
    while len(opentab[0]) > 0:
    #2. Nothing to continue
        if opentab == [[], [], []]:
            return None
    #3. remove n from OPEN
        assert isinstance(opentab[0], list)

        node = opentab[0].pop(0)
        nodevalue = opentab[1].pop(0)
        father = opentab[2].pop(0)
        close[0].append(node)
        close[1].append(nodevalue)
        close[2].append(father)
    #4. Got result
        if node == target:
            close[0].append(node)
            close[1].append(nodevalue)
            close[2].append(father)
            done = True
            break
    #5. Extract followers
        else:
            nexts = NextStep(node)
            for subnode in nexts:
                newvalue = H_Function(subnode, depth)
                #conditions:
                #6.
                if subnode not in opentab[0] and subnode not in close[0]:
                    opentab[0].append(subnode)
                    opentab[1].append(newvalue)
                    opentab[2].append(node)
                #7.
                if subnode in opentab[0]:
                    idx = opentab[0].index(subnode)
                    if newvalue < opentab[1][idx]:
                        opentab[1][idx] = newvalue
                        opentab[2][idx] = node
                #8.
                if subnode in close[0]:
                    idx = close[0].index(subnode)
                    if newvalue < close[1][idx]:
                        close[1][idx] = newvalue
                        close[2][idx] = node
        #9. sort opentab
        HerusticSort(opentab, 1)
        depth += 1

    return close

Upvotes: 0

Views: 228

Answers (2)

senderle
senderle

Reputation: 151007

The only line in this program that could possibly change opentab[0] to a tuple -- under normal circumstances -- is HeuristicSort. Unfortunately, the source of HeuristicSort isn't here, so I can't be certain that's the problem. I'm posting this as requested, though.

Upvotes: 2

quantum
quantum

Reputation: 3830

Change line 58 opentab[0].append(subnode) to opentab[0].append(list(subnode)) should fix it.

The problem is that subnode is a tuple not a list.

Upvotes: 0

Related Questions