Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Class_Dictionary - Fatal编程技术网

附加到python对象字典中的对象列表

附加到python对象字典中的对象列表,python,list,class,dictionary,Python,List,Class,Dictionary,我正在创建一个类,该类包含第二个对象的列表和第二个对象的总金额值。这本身就是有效的。但是,我需要按类别对它们进行分组,所以我创建了一个字典。这看起来很简单,但是每当我将一个对象附加到一个列表时,它就会附加到字典中的所有列表 class objA: amount = 0 listBs = [] category = "" def __init__(self,category): self.category = category class objB:

我正在创建一个类,该类包含第二个对象的列表和第二个对象的总金额值。这本身就是有效的。但是,我需要按类别对它们进行分组,所以我创建了一个字典。这看起来很简单,但是每当我将一个对象附加到一个列表时,它就会附加到字典中的所有列表

class objA:
    amount = 0
    listBs = []
    category = ""
    def __init__(self,category):
        self.category = category
class objB:
    amount = 0
    category = ""
    otherproperty = ""

mydict = {}

b1 = objB()
b1.amount = 1
b1.category = "foo"
b1.otherproperty = "abc"

if(b1.category not in mydict.keys()):
    mydict[b1.category] = objA(b1.category)
mydict[b1.category].listBs.append(b1)
mydict[b1.category].amount += b1.amount


b2 = objB()
b2.amount = 2
b2.category = "bar"
b2.otherproperty = "xyz"

if(b2.category not in mydict.keys()):
    mydict[b2.category] = objA(b2.category)
mydict[b2.category].listBs.append(b2)
mydict[b2.category].amount += b2.amount

print("foo amount: " + str(mydict["foo"].amount) + " - foo len: " + str(len(mydict["foo"].listBs)))
print("bar amount: " + str(mydict["bar"].amount) + " - bar len: " + str(len(mydict["bar"].listBs)))
当我运行上面的代码时,我得到了foo的预期数量1和bar的预期数量2,但是这两个列表的len都是2,因为它们都包含b1和b2

我取出了类对象,同样的原理只是附加到一个列表中,所以下面的工作

dictoflists = {}
dictoflists["key1"] = []
dictoflists["key1"].append("k1v1")
dictoflists["key2"] = []
dictoflists["key2"].append("k2v1")
dictoflists["key2"].append("k2v2")
print(dictoflists)
输出:

{'key1': ['k1v1'], 'key2': ['k2v1', 'k2v2']}

有什么方法可以让它起作用还是有更好的解决方案?

更改
objA
类的定义,以便变量是在实例之前定义的,而不是在类本身上定义的

class objA:
    def __init__(self, category):
        self.category = category
        self.amount = 0
        self.listBs = []

更改
objA
类的定义,以便变量是在实例之前定义的,而不是在类本身上定义的

class objA:
    def __init__(self, category):
        self.category = category
        self.amount = 0
        self.listBs = []

我已经初始化了构造函数中的变量-

class objA:
        amount = 0

        category = ""
        def __init__(self,category):
            self.category = category
      #Initialize the variable inside constructor     
            self.listBs = []
class objB:
    amount = 0
    category = ""
    otherproperty = ""

mydict = {}

b1 = objB()
b1.amount = 1
b1.category = "foo"
b1.otherproperty = "abc"

if(b1.category not in mydict.keys()):
    mydict[b1.category] = objA(b1.category)
mydict[b1.category].listBs.append(b1)
mydict[b1.category].amount += b1.amount


b2 = objB()
b2.amount = 2
b2.category = "bar"
b2.otherproperty = "xyz"

if(b2.category not in mydict.keys()):
    mydict[b2.category] = objA(b2.category)
mydict[b2.category].listBs.append(b2)
mydict[b2.category].amount += b2.amount

print("foo amount: " + str(mydict["foo"].amount) + " - foo len: " + str(len(mydict["foo"].listBs)))
print("bar amount: " + str(mydict["bar"].amount) + " - bar len: " + str(len(mydict["bar"].listBs)))

我已经初始化了构造函数中的变量-

class objA:
        amount = 0

        category = ""
        def __init__(self,category):
            self.category = category
      #Initialize the variable inside constructor     
            self.listBs = []
class objB:
    amount = 0
    category = ""
    otherproperty = ""

mydict = {}

b1 = objB()
b1.amount = 1
b1.category = "foo"
b1.otherproperty = "abc"

if(b1.category not in mydict.keys()):
    mydict[b1.category] = objA(b1.category)
mydict[b1.category].listBs.append(b1)
mydict[b1.category].amount += b1.amount


b2 = objB()
b2.amount = 2
b2.category = "bar"
b2.otherproperty = "xyz"

if(b2.category not in mydict.keys()):
    mydict[b2.category] = objA(b2.category)
mydict[b2.category].listBs.append(b2)
mydict[b2.category].amount += b2.amount

print("foo amount: " + str(mydict["foo"].amount) + " - foo len: " + str(len(mydict["foo"].listBs)))
print("bar amount: " + str(mydict["bar"].amount) + " - bar len: " + str(len(mydict["bar"].listBs)))

objA.listBs
已在类本身上定义。每次从
objA
的任何实例引用
listBs
时,都将引用同一个实例list@IainShelvington如果objA的每个实例都有自己的listb,如何实现所需的行为?是否有其他我不知道的方法来定义它?
objA.listBs
已在类本身上定义。每次从
objA
的任何实例引用
listBs
时,都将引用同一个实例list@IainShelvington如果objA的每个实例都有自己的listb,如何实现所需的行为?有没有其他我不知道的方法来定义它?我刚刚意识到我忘了把它标记为正确答案。谢谢你的帮助。我刚刚意识到我忘了把这个标记为正确答案。谢谢你的帮助。