python怎么去除空字符串
有几种方法可以去除字符串中的空字符串:
- 使用循环和条件语句:
def remove_empty_strings(strings):result = []for string in strings:if string != "":result.append(string)return result
- 使用列表推导式:
def remove_empty_strings(strings):return [string for string in strings if string != ""]
- 使用filter函数:
def remove_empty_strings(strings):return list(filter(lambda string: string != "", strings))
可以通过调用这些函数来去除字符串中的空字符串,例如:
strings = ["hello", "", "world", "", "python"]result = remove_empty_strings(strings)print(result)
输出:
['hello', 'world', 'Python']
文章推荐
-
Python实现字典的key和values的交换
有些时候我们不得已要利用values来反向查询key,有没有简单的方法呢?下面我给大家列举一些方法,方便大家使用pyth...
Python
3个月前 (02-11) 248 -
使用Python脚本来获取Cisco设备信息的示例
今天发现一个使用python写的管理cisco设备的小框架tratto,可以用来批量执行命令。下载后主要有3个文件:Sy...
Python
3个月前 (02-11) 134 -
Python的Django中django-userena组件的简单使用教程
利用twitter/bootstrap,项目的基础模板算是顺利搞定。接下来开始处理用户中心。用户中心主要包括用户登陆、注...
Python
3个月前 (02-11) 125 -
零基础写python爬虫之神器正则表达式
接下来准备用糗百做一个爬虫的小例子。但是在这之前,先详细的整理一下Python中的正则表达式的相关内容。正则表达式在Py...
Python
3个月前 (02-11) 137 -
零基础写python爬虫之抓取百度贴吧代码分享
这里就不给大家废话了,直接上代码,代码的解释都在注释里面,看不懂的也别来问我,好好学学基础知识去! 代码如下:# -*-...
Python
3个月前 (02-11) 125
最新文章
- 微信三级分销系统产品详情页怎么开发? 2个月前 (03-13)
- 微信公众号-获取用户信息(网页授权获取)实现步骤 2个月前 (03-13)
- php微信公众号开发(2)百度BAE搭建和数据库使用 2个月前 (03-13)
- php微信公众号开发(3)php实现简单微信文本通讯 2个月前 (03-13)
- php微信公众号开发(4)php实现自定义关键字回复 2个月前 (03-13)
- php版微信小店调用api示例代码 2个月前 (03-13)
- php版微信小店API二次开发及使用示例 2个月前 (03-13)
- C# 微信公众号开发--准备工作 2个月前 (03-13)
- C#微信公众号开发--微信事件交互 2个月前 (03-13)
- C#微信公众号开发--网页授权(oauth2.0)获取用户基本信息一 2个月前 (03-13)