【发布时间】:2020-03-30 22:54:07
【问题描述】:
我有一个看起来像这样的函数 类数据: def init(self, x, y): """ (数据、列表、列表) -> NoneType
Create a new data object with two attributes: x and y.
"""
self.x = x
self.y = y
def num_obs(self):
""" (Data) -> int
Return the number of observations in the data set.
>>> data = Data([1, 2], [3, 4])
>>> data.num_obs()
2
"""
return len(self.x)
def __str__(self):
""" (Data) -> str
Return a string representation of this Data in this format:
x y
18.000 120.000
20.000 110.000
22.000 120.000
25.000 135.000
26.000 140.000
29.000 115.000
30.000 150.000
33.000 165.000
33.000 160.000
35.000 180.000
"""
for i in range(len(self.x)):
a = self.x[i]
for i in range(len(self.y)):
b = self.y[i]
return ("x y\n{0:.3f} {1:.3f}".format(a,b))
当我调用这个函数时,它只返回列表的最后一个数字。
例如:
数据 = 数据([18,20,22,25],[120,110,120,135])
打印(数据)
x y
25.000 135.000
我希望它返回所有数字
【问题讨论】:
-
将结果合并成一个字符串。您将丢弃除最后一行之外的所有内容。
标签: python python-3.x list