【发布时间】:2022-11-14 18:48:40
【问题描述】:
我想在列表列表中的每个元素周围添加空格
data = [["hello", "world"], ["python", "is", "cool"]]
-->
data = [[" hello ", " world "], [" python ", " is ", " cool "]]
data_new = ["hello world", "python is cool"]
data_new2 = [x.split(" ") for x in data_new]
--> [["hello", "world"], ["python", "is", "cool"]]
data_new2 = [' {0} '.format(word) for elem in data_new2 for word in elem]
print(data_new2[:10])
--> [" hello ", " world ", " python ", " is ", " cool "]
【问题讨论】:
标签: python format list-comprehension whitespace