试图附加到空列表

时间:2016-03-08 14:19:32

标签: python-2.7

我试图附加空grid_list,根据难度(与问题无关)应该包含9或15个单词:

def list_function(): #creates the list that will be used for the grid from file_content_list
    file_content_list = import_function()
    grid_list = [[,,],[,,],[,,]] #this is the list used to make the grid
    counter = 0 #defines counter which allows iteration
    place = 0 #defines place which is what affects the place in grid_list that the word is added to
    if counter < len(file_content_list): #counter decides how many times the next part loops
        for word in file_content_list:
            grid_list[0][place].append(word) #appends the current word to grid_list at the place of value of place
            place += 1 #adds 1 to place
            counter += 1 #adds 1 to counter
            file_content_list.remove(word) #removes the used word
            print grid_list
    elif counter >= 3 and counter < 6: #if counter is equal to 4, 5 or 6
        for word in file_content_list:
            grid_list[0][place].append(word) #appends the current word to grid_list at the place of value of place
            place += 1
            counter += 1
            file_content_list.remove(word)
            print grid_list
    else:
        for word in file_content_list:
            grid_list[0][place].append(word) #appends the current word to grid_list at the place of value of place
            place += 1
            counter += 1
            file_content_list.remove(word)
            print grid_list
    return grid_list

这会出现错误:

  

AttributeError:'str'对象没有属性'append'

1 个答案:

答案 0 :(得分:0)

当你写grid_list[0][place]正在访问元素时。这个元素是一个字符串,无法附加。

grid_list[0][place] = word将单词放在此位置。