简体   繁体   English

如何打印列表的所有值?

[英]How to print all the values of the list?

I have a function which looks like thus class Data: def init (self, x, y): """ (Data, list, list) -> NoneType我有一个函数看起来像这样 class Data: def init (self, x, y): """ (Data, list, list) -> 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))

When I call this function it returns only the last number of the list.当我调用这个函数时,它只返回列表的最后一个数字。

for example:例如:

data = Data([18,20,22,25],[120,110,120,135])数据 = 数据([18,20,22,25],[120,110,120,135])

print(data)打印(数据)

xy xy

25.000 135.000 25.000 135.000

I want it to return all the numbers我希望它返回所有数字

Your question requires joining strings together;您的问题需要将字符串连接在一起; not printing them.不打印它们。 You can use the join method with a generator expression:您可以将join方法与生成器表达式一起使用:

    def __str__(self):
        return 'x               y\n' + '\n'.join(
            '{0:.3f}         {1:.3f}'.format(a, b)
            for a, b in zip(self.x, self.y)
        )

This uses zip as a simpler way to get pairs of numbers at the same index in self.x and self.y .这使用zip作为一种更简单的方法来获取self.xself.y相同索引处的数字对。

Solution解决方案

Change the method __str__() into the following and it would work.将方法__str__()更改为以下内容,它会起作用。

Changed made are:更改为:

  1. Use of zip(self.x, self.y) instead of range(len(self.x)) .使用zip(self.x, self.y)而不是range(len(self.x))
  2. You were returning the last values of a and b in the string.您正在返回字符串中ab的最后一个值。
  3. What you needed instead is a string developed using a loop, where the a,b values get appended to the string in every iteration.您需要的是使用循环开发的字符串,其中a,b值在每次迭代中附加到字符串。
    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
        """
        s = "x               y"
        for a,b in zip(self.x, self.y): 
            s += "\n{0:.3f}         {1:.3f}".format(a,b)
        return s

Output:输出:

data = Data([18,20,22,25],[120,110,120,135])
print(data)

x               y
18.000         120.000
20.000         110.000
22.000         120.000
25.000         135.000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM