2015-05-10 32 views
-4

名單我有如下一個Agent類:附加價值,從特定類型

import Point 
import random 

class Agent(object): 
    """description of class""" 

    locationX = 0 
    locationY = 0 

    def __init__(self, point = None): 
     self.locationX = point.x 
     self.locationY = point.y 

    def GenerateAgents(numberOfAgents): 
     agentList = [] 
     while len(agentList) < numberOfAgents: 

      point = Point.Point() 
      point.x = random.randint(0, 99) 
      point.y = random.randint(0, 99) 

      agent = Agent(point) 
      agentList.append(agent) 
     return agentList 

    def AppendValue(agentList): 
     for item in agentList: 
      item.append(False) 
     return agentList 

    def GetAgentCoordinate(agentList, agentIndex): 
     for agent in agentList: 
      return agentList[agentIndex] 

除了Point類就像這樣:

import math 

class Point(object): 
    """description of class""" 

    x = 0 
    y = 0 

    def __init__(self, x = None, y = None): 
     self.x = x 
     self.y = y 

    def GetDistance(point1, point2): 
     return math.sqrt(math.pow(point1.x - point2.x, 2) + 
         math.pow(point1.y - point2.y)) 

這裏是Main類:

import Agent 
import Point 

if __name__ == "__main__": 

    agentList = Agent.Agent.GenerateAgents(100) 
    selectedAgent = Agent.Agent.GetAgentCoordinate(agentList, 10) 
    myList = Agent.Agent.AppendValue(agentList) //ERROR! 

我打算附加False值到每個子列表agentList。但這是追溯。 Appendvalue已在我的Agent類中定義,append已被考慮到list,但我沒有找到問題的答案。

這聽起來可能是一個愚蠢的錯誤......你能否清理案子?

Traceback (most recent call last): 
File "C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.1\visualstudio_py_util.py", line 106, in 
exec_file 
exec_code(code, file, global_variables) 
File "C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.1\visualstudio_py_util.py", line 82, in exec_code 
exec(code_obj, global_variables) File "C:\Users\Matinking\documents\visual studio 2013\Projects\NeuroSimulation\NeuroSimulation\Main.py", line 8, in <module> 
myList = Agent.Agent.AppendValue(agentList) File "C:\Users\Matinking\documents\visual studio 2013\Projects\NeuroSimulation 
\NeuroSimulation\Agent.py", line 28, in AppendValue 
item.append(False) 
AttributeError: 'Agent' object has no attribute 'append' 
Press any key to continue . . . 
+0

首先,您應該提供一個_minimal_示例來說明問題。其次,我認爲錯誤信息很清楚,你的問題到底是什麼? – kirelagin

+0

'agentList'不是列表的列表。它是一個'Agent'對象列表('agentList.append(agent)')。 –

+0

你正在使用完全錯誤的類。 1)不要在函數之外定義變量'locationX'和'locationY' - 實際上你在這裏創建類方法,這不是你想要的。 2)創建* every *類方法'self'的第一個參數。如果你想要靜態方法,可以使用'@ staticmethd'裝飾器,或者更好的是使用普通函數! –

回答

3

我認爲錯誤是在這裏:

def AppendValue(agentList): 
    for item in agentList: 
     item.append(False) #Precisely here 
    return agentList 

您試圖執行是列表的方法,而itemagentList的元件(未列表)(這是一個列表代理對象)

而且,我看你可以優化下面的代碼:

def GetAgentCoordinate(agentList, agentIndex): 
     for agent in agentList: 
      return agentList[agentIndex] 

只需:

def GetAgentCoordinate(agentList, agentIndex): 
    return agentList[agentIndex] 
+0

感謝您的聲明和提示...其實每個「項目」本身都應該是一個列表。事實上,我試圖以列表清單的方式定義'agentList' ......我幾乎可以處理這種情況? – Roboticist

+1

列表列表的概念很簡單,您只需將主列表的元素作爲列表來思考,就像您將整數列表的元素看作整數一樣,例如: L1 = [1,2,3] L2 = [ 'A', 'b', 'C'] L3 = [真,假,真] L = [] L.append(L1) L.append(L2) L.append(L3) # L將[[1,2,3],['a','b','c'],[True,False,True]] –

+0

我嘗試過'agentList.append([agent])'在追加'False'後,所有元素,即'locationX','locationY'和'False'不在同一個列表級別。我的意思是結果是[[lx,ly],False],而不是[lx,ly,False] ... – Roboticist