You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

二维列表反转后printList函数逐字符输出的原因排查求助

二维列表反转后printList函数逐字符输出的原因排查求助

我最近在写一段代码,想用两个函数实现元素顺序的反转。但遇到个棘手的问题:原本能正常打印多个列表(元素间用空格分隔)的printList函数,现在打印反转后的列表时,居然在每个字母之间都加了空格。有没有大佬能帮我分析下这是为啥?

我的代码如下:

cities = [["Miami", "Atlanta", "Dallas", "Los Angeles"], ["New York", "Chicago", "Portland", "Sacramento"]]

def printList(nightwatch):
    for r in nightwatch:  
        for c in r:        
            print(c, end=' ')  
def flipOrder(listy):
    temp = []
    for r in range (len(listy)):
        for c in range (len(listy[r])):
            temp.insert (0, listy[r][c])
    listy = temp
    printList(listy)
    
printList(cities)
print ()
print ()
print ()
flipOrder(cities)

我试过把printList函数整合到flipOrder里,也调整过循环的c和r变量,但通常这么改代码直接就跑不起来了。我现在最想搞懂的核心问题是:为啥反转后的列表会被拆成单个字符来处理?

备注:内容来源于stack exchange,提问作者formentera lady

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 09:19:50