在 python 中用字符串值拆分嵌套列表

问题描述 投票:0回答:0

我如何能够进行列表理解,将输入列表与

'X'
字符串作为分隔符连接起来。请注意,预期的输出将是字符串。下面的代码不起作用,请修改它。

val = [['_', '_', '_', '_'], ['_', '_', '_', '_', '_']]
output = ' '.join([cols if c != len(row)-1 else cols + 'X' for row in enumerate(formatted_list) for c,cols in enumerate(row)])
print(output)

输入:

val = [['_', '_', '_', '_'], ['_', '_', '_', '_', '_']]

输出:

_ _ _ _ X _ _ _ _ _ 

输入:

val = [['t', 'python'], [2, 6, 7], [9, 5]]

输出:

t X python X 2 6 7 X 9 5
python python-3.x string list list-comprehension
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.