python:如何从列表中打印单独的行?

标签 python python-3.x class append

这是我的代码:

class Message:

    def __init__(self,sender,recipient):
        self.sender = sender
        self.recipient = recipient
        self.wishList = []

    def append(self,line):
        self.wishList.append(str(line))
        for i in line:
            return i

    def toString(self):
        return 'From:{}\nTo:{}\n{}'.format(self.sender,self.recipient,
                                           self.wishList)

输出是:

From:Aria

To:Santa

['For Christmas, I would like:', 'Video games', 'World peace']

如何分隔行并使输出如下?

From:Aria

To:Santa

For Christmas, I would like:

Video games

World peace

最佳答案

您可以通过"joinerchar".join(list)将列表转换为字符串。在您的代码中它将是这样的。

return 'From:{}\nTo:{}\n{}'.format(self.sender,self.recipient,
                                       "\n".join(self.wishList))

关于python:如何从列表中打印单独的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51334640/

相关文章:

python - 在已部署的 Web 应用程序上调用 shell 命令时出现意外行为

python - 如何将字符串转换为单个字符元组列表?

c++ - 如何使用指针 vector 实现类析构函数 C++

arrays - 如何将图像从另一个类传输到 UIImage 数组?

php - 这是一个有效的类变量吗? $this->$变量名

python - 使用 pandas 获取该行中第一个非零值的列名

python - Atom.io 中类似 Jupyter 的文档字符串 View

python - USB串口数据发送乱码

python - 如何在 python 中的多类分类问题上获取每个类的 SHAP 值

python-3.x - OSError : 269892000 requested and 269188084 written