简体   繁体   English

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

[英]python: how to print separate lines from a list?

This is my code: 这是我的代码:

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)

and the output is: 输出为:

From:Aria

To:Santa

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

How can I separate the lines and make the output as following? 如何分隔线并进行如下输出?

From:Aria

To:Santa

For Christmas, I would like:

Video games

World peace

You can convert list to string by "joinerchar".join(list) . 您可以通过"joinerchar".join(list)将列表转换为字符串。 In your code it will be like this. 在您的代码中将是这样。

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

Imagine your array is arr 想象一下你的数组是arr

Then do "\\n".join(arr) it basically takes the array and inserts a new line between each one. 然后执行"\\n".join(arr)它基本上接收数组并在每个数组之间插入新行。

With that example, you should be able to figure it out :) Comment if you need more help. 通过该示例,您应该能够弄清楚:)如果需要更多帮助,请发表评论。

First, some observations: 首先,一些观察:

  1. why are you returning the first character after adding an element to your wishList? 为什么将元素添加到愿望清单后返回第一个字符? (I consider it useless, so I have removed it from my answer) (我认为它没有用,因此已将其从答案中删除)

  2. I think you should add For Christmas, I would like: as part of your template to print. 我想您应该为圣诞节添加作为要打印的模板的一部分。

  3. using str.join() is probably the best alternative to concatenate the elements of the wishList. 使用str.join()可能是连接wishList元素的最佳选择。

  4. Have you ever heard of PEP-8 , basically it seems that you come from a language as Java or C# since you are using upperCamelCase, in Python _ is preferred. 你听说过的PEP-8 ,基本上看来你从语言Java或C#来了,因为你正在使用upperCamelCase,在Python _是首选。

  5. I don't think that converting line to str is relevant assuming you are going to enter a string, but let's leave it. 我认为假设要输入一个字符串,将行转换为str并不重要,但让我们离开吧。

Assuming you are using Python 3.6+, you can use string interpolation: 假设您使用的是Python 3.6+,则可以使用字符串插值:

class Message:
    def __init__(self, sender, recipient):
        self.sender = sender
        self.recipient = recipient
        self.wish_list = []

    def append(self, line):
        self.wish_list.append(str(line))

    def toString(self):
        nl = '\n'
        return f'From: {self.sender}{nl}To: {self.recipient}{nl}For Christmas, I would like:{nl}{nl.join(self.wish_list)}'

If you run the following code: 如果运行以下代码:

m = Message('Aria', 'Santa')
m.append('Video games')
m.append('World peace')
print(m.toString())

The output will be: 输出将是:

From: Aria
To: Santa
For Christmas, I would like:
Video games
World peace

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

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