Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将值从特定类型追加到列表_Python - Fatal编程技术网

Python 将值从特定类型追加到列表

Python 将值从特定类型追加到列表,python,Python,我有一个代理课程,如下所示: 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 Gene

我有一个
代理
课程,如下所示:

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]
以下是
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
已在
列表中考虑,但我没有找出问题所在

这听起来可能是个愚蠢的错误。。。请你清理一下这个箱子好吗

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 . . .
回溯(最近一次呼叫最后一次):
文件“C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.1\visualstudio\u py\u util.py”,第106行,中
执行文件
执行代码(代码、文件、全局变量)
exec\U代码中的文件“C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.1\visualstudio\u py\u util.py”,第82行
exec(代码对象,全局变量)文件“C:\Users\Matinking\documents\visual studio 2013\Projects\NeuroSimulation\NeuroSimulation\Main.py”,第8行,在
myList=Agent.Agent.AppendValue(agentList)文件“C:\Users\Matinking\documents\visual studio 2013\Projects\neuromulation
\神经刺激\Agent.py“,附录值第28行
item.append(False)
AttributeError:“代理”对象没有属性“附加”
按任意键继续。

我认为错误在这里:

def AppendValue(agentList):
    for item in agentList:
        item.append(False) #Precisely here
    return agentList
您正在尝试执行一个用于列表的方法,而
代理列表
(代理对象列表)中的一个元素(不是列表)

此外,我发现您可以优化以下代码:

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

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

首先,您应该提供一个演示问题的最小示例。其次,我认为错误信息非常清楚,您的问题到底是什么?
agentList
不是一个列表列表。它是
Agent
对象的列表(
agentList.append(Agent)
)。您使用的类完全错误。1) 不要在函数外部定义变量
locationX
locationY
——实际上您在这里创建的是类方法,而这不是您想要的。2) 使每个类方法的第一个参数
self
。如果您想要静态方法,请使用
@staticmethd
装饰器,或者更好地使用普通函数!谢谢你的声明和提示。。。实际上,每个
项目本身都应该是一个列表。事实上,我试图将
agentList
定义为一个列表列表。。。实际上,我该如何处理这种情况呢?列表列表的概念很简单,你只要把主列表的元素看作列表,就像你把整数列表的元素看作整数一样,例如:L1=[1,2,3]L2=['a','b','c']L3=[True,False,True]L=[]L.append(L1)L.append(L2)L.append(L3)#L将是[[1,2,3],'a','b','c'],[True,False,True]]我试过使用
agentList.append([agent])
但是在追加
False
之后,所有元素,即
locationX
locationY
False
都不在同一个列表级别。我的意思是结果将是[[lx,ly],False],而不是[lx,ly,False]..你能用修改后的代码再次发布吗?这个问题已经用
extend
方法解决了,而不是
append
方法…谢谢
def GetAgentCoordinate(agentList, agentIndex):
    return agentList[agentIndex]