gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:46:04 26 4
gpt4 key购买 nike

这是我的代码:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com