【发布时间】:2022-12-02 22:58:16
【问题描述】:
我尝试使用 reduce 函数将字符串列表组合到字符串,但它不起作用。无论如何,我更喜欢使用 reduce 函数,我该如何解决这个问题?
>> reduce(lambda x, y: x + y + "\n", ["dog", "cat"])
# this doesn't work
# dogcat
>> "\n".join(["dog", "cat"])
# this works
# dog
# cat
【问题讨论】:
-
当然不行。您只在末尾添加一个新行。你可能是说
x + '\n' + y -
@mousetail 有用哇谢谢!
-
@mousetail 嗨! :)
标签: python