python - 如何将列表列表转换为带有新行的字符串
问题描述
我有这个代码:
def table(h, w):
table = [['.' for i in range(w)] for j in range(h)]
return table
返回这个
[
['.', '.', '.', '.'], ['.', '.', '.', '.'], ['.', '.', '.', '.'], ['.', '.', '.', '.']
]
如何让它返回这个?
....
....
....
....
解决方案
你不能直接返回这样的东西。这与任何数据类型无关。但是,您可以创建一个函数来格式化输出,就像给定您当前的表表示形式一样:
def print_table(table):
for row in table:
print(''.join(row))
这为您提供了所需的输出:
....
....
....
....
编辑:现在我考虑一下,这是可能的。但是你只会返回你的表的字符串表示,而不是它的结构(我假设你需要):
def table(h, w):
table = ''
for row in range(h):
table += '.' * w + '\n'
return table
推荐阅读
- sql-server - 查询列出表值函数及其输入参数?
- deep-learning - Hugging Face Transformers 上的“TextInputSequence must be str”错误
- c# - 如何在 EF Core 中使用 C# 9 记录?
- r - 是否有一种算法可以删除两个单词之间的破折号(“-”)然后收缩它们?
- wordpress - Woocommerce 我的帐户页面上缺少无效的凭据提示
- javascript - 使用 firestore 生成我们自己的 id 文档
- python - Discordpy 在 var 更改时发送 dms 但收到 AttributeError: 'NoneType' object has no attribute 'send'
- amazon-web-services - 如何使用 AWS Glue ETL 工具从 S3 读取文件名
- reactjs - i18n 翻译库在 nextJS 的某些页面中不起作用
- syntax-error - Matlab 绘图图