我想使用map函數來計算質心。我不想使用for循環。幫助底部兩行?使用map處理python中的對象列表
class Obj():
def __init__(self, mass = 0., x = 0., y = 0.):
self.mass = mass
self.x = x
self.y = y
# Create List of Objects
objList = []
n = 0
for i in range(0,10):
for j in range(0,10):
objList.append(Obj(i*j,i,j))
# Calculate Center of Mass of List
# The following is pseudocode, does not actually work
SumOfMass = sum(objList[:].mass)
CenterOfMassX = sum(objList[:].x.*objList[:].mass)/SumOfMass
它只是在py2k for循環。在py3k中,它是一個迭代器。 – 2010-10-15 11:17:20
'map'是一個更高級別的函數。 – intuited 2010-10-15 11:19:10
@intuited:是嗎?好。和? – SilentGhost 2010-10-15 11:20:04