I am new in Python and using python3. I have list og objects of type points
class Po:
def __init__(self, x: int, y: int):
self.x = x
self.y = y
C = [Po(0, j) for j in range(10)]
and 2 dimensional array
m = [[j for i in range(2)] for j in range(10)]
I want to assign all fields x of C to values with index 0 from m like
C[:].x = m[:][0]
but python says list doesn't have field x. how can I do this and why I can' access x this way
CodePudding user response:
There's no specific syntax for this in Python. You'll just need to use a regular for
loop.
for ci, mi in zip(C, m):
ci.x = mi[0]
CodePudding user response:
Your "C" instance is itself a list, so in order to access it's x or y values, you need to call the index first. So this way you can access them:
for i in range(10):
print(C[i].x)
print(C[i].y)