我有一个列表,其中包含Python中大型列表的每个项目中的URL和一些文本。我想在每次出现空格时将每个项目分成几个项目(每个项目2-3个空格)。发布的代码不多,它只是一个存储在命名变量中的列表。我已经尝试过使用split功能,但我似乎无法正确使用它。任何帮助将不胜感激!
答案 0 :(得分:7)
很难知道你要求的是什么,但我会试一试。
>>> a = ['this is', 'a', 'list with spaces']
>>> [words for segments in a for words in segments.split()]
['this', 'is', 'a', 'list', 'with', 'spaces']
答案 1 :(得分:0)
你可以尝试这样的事情:
>>> items = ['foo bar', 'baz', 'bak foo bar']
>>> new_items = []
>>> for item in items:
... new_items.extend(item.split())
...
>>> new_items
['foo', 'bar', 'baz', 'bak', 'foo', 'bar']