方法1:使用列表推导式 + str()
# 包含非字符串元素的列表 my_list = [1, 2, 3, 4, 5] # 方法1:列表推导式 result = ','.join([str(x) for x in my_list]) print(result) # 输出: "1,2,3,4,5"
方法2:使用 map() 函数
# 方法2:map函数 result = ','.join(map(str, my_list)) print(result) # 输出: "1,2,3,4,5"
方法3:先转换再连接
# 方法3:先创建字符串列表 str_list = [] for item in my_list: str_list.append(str(item)) result = ','.join(str_list) print(result) # 输出: "1,2,3,4,5"