Search code examples
python-2.7appendattributeerror

while trying to append I receive an attributeerror


I'm stuck with the following. I seem to be doing something wrong while appending. I receive the error: 'float' object has no attribute 'append'. I understand now I'm appending a float value, which isn't possible. But how can I get for example in TheorBlockNeighborsOne in each list on the first index the value of index [0] of Data_BM_Sorted_List, and in each list on the second spot the value of index 1 from Data_BM_Sorted_List and on the third spot the value of Data_BM_Sorted_List at index 2. And then for each i.

TheorBlockNeighborsOne[i] = Data_BM_Sorted_List[i][0]                                        
TheorBlockNeighborsOne[i].append((Data_BM_Sorted_List[i][1]+Horizontal_Block_Dimensions))
TheorBlockNeighborsOne[i].append((Data_BM_Sorted_List[i][2]))

Horizontal_Block_Dimensions = int(raw_input())

The Raw Input for Horizontal_Block_Dimensions is 100

Data_BM_Sorted_List = [[336350.0, 7089650.0, -7.0, 0.1665, 1.5, 1], [336350.0, 7089650.0, -5.0, 0.1542, 1.5, 1], [336350.0, 7089650.0, -3.0, 0.2259, 1.5, 1], [336350.0, 7089650.0, -1.0, 0.2753, 1.5, 1], [336350.0, 7089650.0, 1.0, 0.2236, 1.5, 1], [336450.0, 7089550.0, -7.0, 0.1791, 1.5, 2], [336450.0, 7089550.0, -5.0, 0.1707, 1.5, 2], [336450.0, 7089550.0, -3.0, 0.2248, 1.5, 2], [336450.0, 7089550.0, -1.0, 0.2791, 1.5, 2], [336450.0, 7089550.0, 1.0, 0.3098, 1.5, 2], [336450.0, 7089650.0, -5.0, 0.1488, 1.5, 3], [336450.0, 7089650.0, -3.0, 0.1961, 1.5, 3], [336450.0, 7089650.0, -1.0, 0.2499, 1.5, 3], [336450.0, 7089650.0, 1.0, 0.2939, 1.5, 3], [336550.0, 7089350.0, -7.0, 0.1774, 1.5, 4], [336550.0, 7089350.0, -5.0, 0.2551, 1.5, 4], [336550.0, 7089350.0, -3.0, 0.3489, 1.5, 4], [336550.0, 7089350.0, -1.0, 0.3707, 1.5, 4], [336550.0, 7089350.0, 1.0, 0.3037, 1.5, 4], [336550.0, 7089450.0, -5.0, 0.1719, 1.5, 5], [336550.0, 7089450.0, -3.0, 0.3121, 1.5, 5], [336550.0, 7089450.0, -1.0, 0.3491, 1.5, 5], [336550.0, 7089450.0, 1.0, 0.326, 1.5, 5], [336550.0, 7089550.0, -7.0, 0.1494, 1.5, 6], [336550.0, 7089550.0, -5.0, 0.1598, 1.5, 6], [336550.0, 7089550.0, -3.0, 0.2061, 1.5, 6], [336550.0, 7089550.0, -1.0, 0.2554, 1.5, 6], [336550.0, 7089550.0, 1.0, 0.3218, 1.5, 6], [336550.0, 7089650.0, -5.0, 0.1334, 1.5, 7], [336550.0, 7089650.0, -3.0, 0.1711, 1.5, 7], [336550.0, 7089650.0, -1.0, 0.193, 1.5, 7], [336550.0, 7089650.0, 1.0, 0.2498, 1.5, 7], [336650.0, 7089150.0, -9.0, 0.165, 1.5, 8], [336650.0, 7089150.0, -7.0, 0.1791, 1.5, 8], [336650.0, 7089150.0, -5.0, 0.2482, 1.5, 8], [336650.0, 7089150.0, -3.0, 0.3541, 1.5, 8]]

TheorBlockNeighborsOne = [[]]*len(Data_BM_Sorted_List)
TheorBlockNeighborsTwo = [[]]*len(Data_BM_Sorted_List)
TheorBlockNeighborsThree = [[]]*len(Data_BM_Sorted_List)
TheorBlockNeighborsFour = [[]]*len(Data_BM_Sorted_List)
TheorBlockNeighborsFive = [[]]*len(Data_BM_Sorted_List)
TheorBlockNeighborsSix = [[]]*len(Data_BM_Sorted_List)
TheorBlockNeighborsSeven = [[]]*len(Data_BM_Sorted_List)
TheorBlockNeighborsEight = [[]]*len(Data_BM_Sorted_List)


# BlockNeighborsX = [[X,Y,Z]]
for i in range(0,len(TheorBlockNeighborsOne)):
    TheorBlockNeighborsOne[i] = Data_BM_Sorted_List[i][0]                                        
    TheorBlockNeighborsOne[i].append((Data_BM_Sorted_List[i][1]+Horizontal_Block_Dimensions))
    TheorBlockNeighborsOne[i].append((Data_BM_Sorted_List[i][2]))
    TheorBlockNeighborsTwo[i] = Data_BM_Sorted_List[i][0] + Horizontal_Block_Dimensions
    TheorBlockNeighborsTwo[i].append(Data_BM_Sorted_List[i][1])
    TheorBlockNeighborsTwo[i].append(Data_BM_Sorted_List[i][2])
    TheorBlockNeighborsThree[i] = Data_BM_Sorted_List[i][0] 
    TheorBlockNeighborsThree[i].append(Data_BM_Sorted_List[i][1] - Horizontal_Block_Dimensions)
    TheorBlockNeighborsThree[i].append(Data_BM_Sorted_List[i][2])
    TheorBlockNeighborsFour[i] = Data_BM_Sorted_List[i][0] - Horizontal_Block_Dimensions
    TheorBlockNeighborsFour[i].append(Data_BM_Sorted_List[i][1])
    TheorBlockNeighborsFour[i].append(Data_BM_Sorted_List[i][2])
    TheorBlockNeighborsFive[i] = Data_BM_Sorted_List[i][0] - Horizontal_Block_Dimensions
    TheorBlockNeighborsFive[i].append(Data_BM_Sorted_List[i][1] + Horizontal_Block_Dimensions)
    TheorBlockNeighborsFive[i].append(Data_BM_Sorted_List[i][2])
    TheorBlockNeighborsSix[i] = Data_BM_Sorted_List[i][0] + Horizontal_Block_Dimensions
    TheorBlockNeighborsSix[i].append(Data_BM_Sorted_List[i][1] + Horizontal_Block_Dimensions)
    TheorBlockNeighborsSix[i].append(Data_BM_Sorted_List[i][2])
    TheorBlockNeighborsSeven[i] = Data_BM_Sorted_List[i][0] + Horizontal_Block_Dimensions
    TheorBlockNeighborsSeven[i].append(Data_BM_Sorted_List[i][1] - Horizontal_Block_Dimensions)
    TheorBlockNeighborsSeven[i].append(Data_BM_Sorted_List[i][2])
    TheorBlockNeighborsEight[i] = Data_BM_Sorted_List[i][0] - Horizontal_Block_Dimensions
    TheorBlockNeighborsEight[i].append(Data_BM_Sorted_List[i][1] - Horizontal_Block_Dimensions)
    TheorBlockNeighborsEight[i].append(Data_BM_Sorted_List[i][2])
print TheorBlockNeighborsOne

The error I get:

AttributeError                            Traceback (most recent call last)
<ipython-input-46-a2f61fb09e5b> in <module>()
     12 for i in range(0,len(TheorBlockNeighborsOne)):
     13     TheorBlockNeighborsOne[i] = Data_BM_Sorted_List[i][0]
---> 14     TheorBlockNeighborsOne[i].append((Data_BM_Sorted_List[i][1]+Horizontal_Block_Dimensions))
     15     TheorBlockNeighborsOne[i].append((Data_BM_Sorted_List[i][2]))
     16     TheorBlockNeighborsTwo[i] = Data_BM_Sorted_List[i][0] + Horizontal_Block_Dimensions

AttributeError: 'float' object has no attribute 'append'

Solution

  • You assign a float to your list index, then try to call .append() on that float:

    TheorBlockNeighborsOne[i] = Data_BM_Sorted_List[i][0]
    TheorBlockNeighborsOne[i].append((Data_BM_Sorted_List[i][1]+Horizontal_Block_Dimensions))
    

    Here, Data_BM_Sorted_List[i] is one of the nested lists from Data_BM_Sorted_List, and [0] the first value in that list, which is always a float:

    Data_BM_Sorted_List = [[336350.0, 7089650.0, -7.0, 0.1665, 1.5, 1], [336350.0, 7089650.0, -5.0, 0.1542, 1.5, 1], [336350.0, 7089650.0, -3.0, 0.2259, 1.5, 1], [336350.0, 7089650.0, -1.0, 0.2753, 1.5, 1], [336350.0, 7089650.0, 1.0, 0.2236, 1.5, 1], [336450.0, 7089550.0, -7.0, 0.1791, 1.5, 2], [336450.0, 7089550.0, -5.0, 0.1707, 1.5, 2], [336450.0, 7089550.0, -3.0, 0.2248, 1.5, 2], [336450.0, 7089550.0, -1.0, 0.2791, 1.5, 2], [336450.0, 7089550.0, 1.0, 0.3098, 1.5, 2], [336450.0, 7089650.0, -5.0, 0.1488, 1.5, 3], [336450.0, 7089650.0, -3.0, 0.1961, 1.5, 3], [336450.0, 7089650.0, -1.0, 0.2499, 1.5, 3], [336450.0, 7089650.0, 1.0, 0.2939, 1.5, 3], [336550.0, 7089350.0, -7.0, 0.1774, 1.5, 4], [336550.0, 7089350.0, -5.0, 0.2551, 1.5, 4], [336550.0, 7089350.0, -3.0, 0.3489, 1.5, 4], [336550.0, 7089350.0, -1.0, 0.3707, 1.5, 4], [336550.0, 7089350.0, 1.0, 0.3037, 1.5, 4], [336550.0, 7089450.0, -5.0, 0.1719, 1.5, 5], [336550.0, 7089450.0, -3.0, 0.3121, 1.5, 5], [336550.0, 7089450.0, -1.0, 0.3491, 1.5, 5], [336550.0, 7089450.0, 1.0, 0.326, 1.5, 5], [336550.0, 7089550.0, -7.0, 0.1494, 1.5, 6], [336550.0, 7089550.0, -5.0, 0.1598, 1.5, 6], [336550.0, 7089550.0, -3.0, 0.2061, 1.5, 6], [336550.0, 7089550.0, -1.0, 0.2554, 1.5, 6], [336550.0, 7089550.0, 1.0, 0.3218, 1.5, 6], [336550.0, 7089650.0, -5.0, 0.1334, 1.5, 7], [336550.0, 7089650.0, -3.0, 0.1711, 1.5, 7], [336550.0, 7089650.0, -1.0, 0.193, 1.5, 7], [336550.0, 7089650.0, 1.0, 0.2498, 1.5, 7], [336650.0, 7089150.0, -9.0, 0.165, 1.5, 8], [336650.0, 7089150.0, -7.0, 0.1791, 1.5, 8], [336650.0, 7089150.0, -5.0, 0.2482, 1.5, 8], [336650.0, 7089150.0, -3.0, 0.3541, 1.5, 8]]
    

    Note that your method to create matrices is flawed, you are falling into the list multiplication trap described in List of lists changes reflected across sublists unexpectedly; multiplication of a list will not create new objects; rather only references the contents are repeated. So TheorBlockNeighborsOne[0] is going to be the exact same list object as TheorBlockNeighborsOne[1], etc. Append a value to that nested list in at one index, and you'll see the same value repeated across all the other references.

    If you wanted to make that floating point value part of the each list, you need to append it too; assignment replaces the list object.

    Next, your code is repeating itself many times. Rather than create 8 named lists, use a dictionary or list to contain them all. You could do this:

    theor_block_neighbors = [
        [[] for _ in range(len(Data_BM_Sorted_List))]
        for _ in range(8)]
    

    That creates a list of 8 matrices, which you can then address in a loop.