python - 在python中将列表打印为不带括号或逗号的字符串

标签 python

我有列表::[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 由循环创建。

我要打印

0 1 2 3 4 5 6 7 8 9

我已经使用 .strip('[]') 去掉了括号,但我无法去掉逗号。

最佳答案

你试图改变列表的代表字符串而不是使用它的成员来构建你的特定 表示。这不是要走的路。

使用map创建字符串和str.join使用您喜欢的分隔符连接字符串:

lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(' '.join(map(str, lst)))

一个带有 generator expression 的替代品:

lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(' '.join((str(x) for x in lst)))

关于python - 在python中将列表打印为不带括号或逗号的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31769525/

相关文章:

python - 如何安装/导入 pyspark-csv?

python - 使用 sqlalchemy 查询特定的 JSON 列(postgres)

python - 这个算法的复杂性用大O表示法?

Python,从Excel列中提取数字并写入输出

python - 为每个级别使用不同的格式分割 matplotlib 标题

python - 在 Mechanical Turk 中创建批处理的方法

python - Pyramid :sqlalchemy.exc.OperationalError

python - 在 python 中创建元组集

python - 单击第二个按钮(相同的按钮)Selenium python

python - 将 pandas 数据帧转换为 Spark 数据帧时收到错误