因为我是Python的新手,所以我对语法很难。考虑我有一个class A
来存储class B
的对象列表。每个类都有一个名为x
的值。在代码的最后,我迭代list of A
比较哪个x
更大并在控制台上打印。
代码:
class A:
def __init__(self, x_a, listB=None):
self.x = x_a
if listB is None:
listB = []
self.listOfBs = listB
class B:
def __init__(self, x_b, cp):
self.x = x_b
self.comparison = cp
ListB = []
for i in range(0,3):
x = B(i, None)
ListB.append(x)
ListA = []
for i in range(0,3):
y = A(i,ListB)
ListA.append(y)
for a in ListA:
for b in a.listOfBs:
if a.x > b.x:
b.comparison = 'A IS BIGGER'
else:
b.comparison = 'A IS NOT BIGGER'
for a in ListA:
for b in a.listOfBs:
print(a.x, b.x, b.comparison)
我收到的输出是:
(0, 0, 'A IS BIGGER')
(0, 1, 'A IS BIGGER')
(0, 2, 'A IS NOT BIGGER')
(1, 0, 'A IS BIGGER')
(1, 1, 'A IS BIGGER')
(1, 2, 'A IS NOT BIGGER')
(2, 0, 'A IS BIGGER')
(2, 1, 'A IS BIGGER')
(2, 2, 'A IS NOT BIGGER')
这是不太令人期待的。
编辑预期输出为:
(0, 0, 'A IS BIGGER')
(0, 1, 'A IS NOT BIGGER')
(0, 2, 'A IS NOT BIGGER')
(1, 0, 'A IS BIGGER')
(1, 1, 'A IS NOT BIGGER')
(1, 2, 'A IS NOT BIGGER')
(2, 0, 'A IS BIGGER')
(2, 1, 'A IS BIGGER')
(2, 2, 'A IS NOT BIGGER')
非常感谢任何帮助。
谢谢。
答案 0 :(得分:1)
所有A类对象都有一个变量self.listOfBs
,它指向同一个精确列表......具有相同的三个B对象。每个A对象没有3个B对象;你有3个B对象。并且每个B对象都有一个b.comparison
变量。
在比较循环中,当您为b.comparison
指定一个值时,您正在为最后一次循环执行的同一b对象进行赋值,覆盖之前的值。
一个简单的解决方法是在A类初始化函数中创建列表的深层副本。每个A对象都有自己的3个NEW B对象列表。现在你的比较循环将为A对象的唯一B对象列表赋值。
import copy
class A:
def __init__(self, x_a, listB=None):
self.x = x_a
if listB is None:
self.listOfBs = []
else:
self.listOfBs = copy.deepcopy(listB)
答案 1 :(得分:0)
每当您设置b.comparison
时,都会覆盖之前的b.comparison
值。如果要在一个属性中存储多个比较,请考虑另一种数据类型,例如dict
离子。
class B:
def __init__(self, x_b, cp):
self.x = x_b
if cp is None:
cp = {}
self.comparison = cp
for a in ListA:
for b in a.listOfBs:
if a.x > b.x:
# Think of it as "b's comparison _with_ a"
b.comparison[a] = 'A IS BIGGER'
else:
b.comparison[a] = 'A IS NOT BIGGER'
for a in ListA:
for b in a.listOfBs:
print(a.x, b.x, b.comparison[a])