在尝试追加时,我收到了一个属性错误

时间:2016-06-19 09:14:13

标签: python-2.7 append attributeerror

我坚持以下。在追加时我似乎做错了什么。我收到错误:' float'对象没有属性'追加'。 我现在明白了我附加一个浮动值,这是不可能的。但是我如何在第一个索引的每个列表中的TheorBlockNeighborsOne中获取Data_BM_Sorted_List的索引[0]的值,并且在第二个点上的每个列表中从Data_BM_Sorted_List获取索引1的值,并在第三个点上获取Data_BM_Sorted_List的值在索引2.然后为每个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())

Horizo​​ntal_Block_Dimensions的原始输入为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

我得到的错误:

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'

1 个答案:

答案 0 :(得分:0)

您为列表索引指定了一个浮点数,然后尝试在该浮点数上调用.append()

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

此处,Data_BM_Sorted_List[i]是来自Data_BM_Sorted_List的嵌套列表之一,[0]是该列表中的第一个值,始终为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]]

请注意,创建矩阵的方法存在缺陷,您将陷入List of lists changes reflected across sublists unexpectedly中描述的列表乘法陷阱;列表的乘法不会创建新对象;而只是引用重复内容。因此TheorBlockNeighborsOne[0]将与TheorBlockNeighborsOne[1]等完全相同的列表对象。在一个索引处向该嵌套列表附加一个值,并且您将看到在所有索引中重复的相同值其他参考文献。

如果您想将浮点值作为每个列表的一部分,则需要将其附加;赋值取代了列表对象。

接下来,您的代码会重复多次。而不是创建8个命名列表,使用字典或列表来包含它们。你可以这样做:

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

这将创建一个包含8个矩阵的列表,然后您可以在循环中进行寻址。