在Python中的空格处拆分列表中的每个字符串 [英] Splitting each string in a list at spaces in Python

查看:1027
本文介绍了在Python中的空格处拆分列表中的每个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python的一个大列表的每个项目中都有一个包含url和一些文本的列表.我想在每次出现空格时将每个项目拆分为几个项目(每个项目2-3个空格).没有太多要发布的代码,此刻只是存储在命名变量中的列表.我试过使用split函数,但似乎无法正确处理.任何帮助将不胜感激!

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']

这篇关于在Python中的空格处拆分列表中的每个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆