python - 如何修改我的 print_list 以使其看起来像 [1, 2, 3]?
问题描述
还有其他我没有包含的代码,因为我认为它不相关
我有的:
def print_list(node):
print('[', end=" ")
while node:
print(node)
if node.next:
print(',', end=" ")
node = node.next
print(']')
我得到以下输出:
[ 1
, 2
, 3
]
它现在已修复,对于任何想要更正版本的人,谢谢 JohanC(:
def print_list(node):
print('[', end="")
while node:
print(node, end="")
if node.next:
print(',', end=" ")
node = node.next
print(']')
print_list(node1)
解决方案
也许你也不想在之后换行print(node)
?
def print_list(node):
print('[', end="")
while node:
print(node, end="")
if node.next:
print(',', end=" ")
node = node.next
print(']')
请注意,print('something')
打印出给定的文本,然后开始新的一行。print(']', end=' ')
不开始一个新行,而是打印出由 给出的字符串end=
,它可以是一个空字符串。
推荐阅读
- javascript - 初始化需要多个静态值的 JavaScript 对象的替代/更好的方法?
- python-3.x - 关于在递归调用结束时返回值的问题
- kubernetes - 有没有办法使用 VS Code 编辑 kubernetes pod 中的代码?
- node.js - 在下面的 JSON 中,我可以通过 cookies.set("key").... 在 cookie 中设置密钥。但是如何从 cookie 中获取值,例如姓名、电子邮件和等级
- android - 如何解决由 Flutter 中的 firebase_messaging 引发的“无法确定任务的依赖关系...”?
- javascript - 单击两次时阻止div隐藏?
- jquery - Jquery DataTables - 使用查找表连接表
- javascript - 一个 JS 中的 2 个函数 init
- algorithm - 为什么伪代码只有一个用于 DFS 算法的数组?
- ios - Firebase Crashlytics iOS:即使脚本似乎正在运行,也无法接收事件