天天看點

python拆分字元串生成清單,在Python中的空格處拆分清單中的每個字元串

python拆分字元串生成清單,在Python中的空格處拆厘清單中的每個字元串

I've got a list that contains a url and some text in each item of a large list in Python. I'd like to split each item in several items every time a space appears (2-3 spaces per item). There isn't much code to post, its just a list stored in a named variable at the moment. I've tried using the split function but I just can't seem to get it right. Any help would be greatly appreciated!

解決方案

It's hard to know what you're asking for but I'll give it a shot.

>>> a = ['this is', 'a', 'list with spaces']

>>> [words for segments in a for words in segments.split()]

['this', 'is', 'a', 'list', 'with', 'spaces']