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

求助:打印列表时无法去除括号,多种方法尝试无效

enter image description here

直接打印列表时出现括号,是因为Python里列表的默认字符串表示(str(list))本身就包含[]括号和逗号分隔符,print()函数输出的就是这个默认格式。

以下是几种常用的去除括号的解决方法:

  • 使用join()方法拼接元素(元素需可转为字符串):

    # 字符串列表
    str_list = ["apple", "banana", "cherry"]
    print(" ".join(str_list))  # 输出: apple banana cherry
    
    # 数字列表需先转换为字符串
    num_list = [10, 20, 30]
    print(", ".join(str(num) for num in num_list))  # 输出: 10, 20, 30
    
  • 使用*解包列表元素,自定义分隔符:

    my_list = ["cat", "dog", "bird"]
    print(*my_list)  # 默认空格分隔,输出: cat dog bird
    print(*my_list, sep="|")  # 自定义分隔符,输出: cat|dog|bird
    
  • 遍历列表逐个打印,控制结尾字符:

    my_list = [1, 2, 3]
    for item in my_list:
        print(item, end=" ")  # 输出: 1 2 3 
    

内容的提问来源于stack exchange,提问作者Glasses_

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 21:06:06