首页 > 解决方案 > 将单个索引列表拆分为多个列表索引?

问题描述

我有一个清单:

lst = ['words in a list']

我希望将字符串中的这些单词中的每一个拆分为它们自己单独的索引。例如,它看起来像这样:

lst = ['words','in','a','list']

我想知道这是否可能?我最初认为这只是一个带有循环的简单 lst.split() ,但似乎这会引发错误。

谢谢您的帮助!

标签: pythonlist

解决方案


用这个:

print(lst[0].split())

如果列表有更多元素:

print([x for i in lst for x in i.split()])

推荐阅读