有时, 在处理python数据时, 我们可能会遇到一个问题, 我们需要过滤掉某些空数据。这些可以是None, 空字符串等。这可以在许多域中应用。让我们讨论可以删除空列表的某些方法

方法1:使用列表理解

这是可以解决此问题的方法之一。在此, 我们遍历列表, 不包括空列表。

# Python3 code to demonstrate
# Remove empty List from List
# using list comprehension# Initializing list
test_list = [ 5 , 6 , [], 3 , [], [], 9 ]# printing original list
print ( "The original list is : " + str (test_list))# Remove empty List from List
# using list comprehension
res = [ele for ele in test_list if ele ! = []]# printing result
print ( "List after empty list removal : " + str (res))

输出:

The original list is : [5, 6, [], 3, [], [], 9]
List after empty list removal : [5, 6, 3, 9]

方法2:使用filter()

这是可以执行此任务的另一种方法。在此, 我们过滤None值。 none值也包括空列表, 因此将其删除。

# Python3 code to demonstrate
# Remove empty List from List
# using filter()# Initializing list
test_list = [ 5 , 6 , [], 3 , [], [], 9 ]# printing original list
print ( "The original list is : " + str (test_list))# Remove empty List from List
# using filter
res = list ( filter ( None , test_list))# printing result
print ( "List after empty list removal : " + str (res))

输出:

The original list is : [5, 6, [], 3, [], [], 9]
List after empty list removal : [5, 6, 3, 9]

更多Python开发相关内容请参考:lsbin - IT开发技术:https://www.lsbin.com/

查看以下更多Python相关的内容:

  • Python使用pytube下载视频:https://www.lsbin.com/3450.html
  • Python进程的同步和池化:https://www.lsbin.com/3326.html
  • Python 2.x和Python 3.x的区别:https://www.lsbin.com/2966.html

Python如何从列表中删除空列表?代码示例相关推荐

  1. java字符串删掉子串_如何从Java中的列表中删除子列表?

    java字符串删掉子串 从列表中删除子列表 (Removing SubList from a List) Suppose, we have a list of few elements like th ...

  2. java list 去空字符串_从字符串列表中删除空字符串

    我想从python中的字符串列表中删除所有空字符串. 我的想法如下: while '' in str_list: str_list.remove('') 有没有更多的Python方式可以做到这一点? ...

  3. python列表中删除子列表_从列表列表中删除列表元素

    你试过删除列表吗?在In [84]: a = [[1, 2, 3, 4], [1, 2, 3, 5], [2, 5, 4, 3], [5, 2, 3, 1]] In [85]: a Out[85]: ...

  4. python列表删除算法_关于算法:如何从python中的列表中删除重复的条目

    最近在一次采访中,我被要求编写一个python代码来从列表中删除所有重复的条目. 例如: Input List = {1,2,4,5,2,3,1} Expected Output List = {4, ...

  5. python文件中单词的删除_python-从文本列表中删除单词

    我正在尝试从文本字符串列表中删除某些单词(除了使用停用词),但是由于某些原因它不起作用 documents = ["Human machine interface for lab abc c ...

  6. Python | 程序从列表中删除范围内的所有元素

    Given a list and we have to remove elements in a range from the list in Python. 给定一个列表,我们必须从Python中的 ...

  7. python里面列表可以同时删除吗_在python中从列表中删除项,同时对其进行迭代

    本问题已经有最佳答案,请猛点这里访问. 我正在为锦标赛应用程序编写循环算法. 当玩家数量为奇数时,我将'DELETE'添加到玩家列表中,但稍后,当我想从包含'DELETE'的日程表列表中删除所有项目时 ...

  8. python从后面删除重复项_如何从Python列表中删除重复项

    如何从Python列表中删除重复项 了解如何从Python中的List中删除重复项技巧. 实例 从列表中删除任何重复项: mylist = ["a", "b", ...

  9. python列表去空值_如何在Python列表中的列表中删除nan / null值? - python

    因此,我有一个具有NaN值的数据框,并将该数据框中的所有行转换为一个列表,然后将其添加到另一个列表中. Index 1 2 3 4 5 6 7 8 9 10 ... 71 72 73 74 75 76 ...

  10. python删除列表中的偶数_Python:从列表中删除奇数

    我写了一个函数来从列表中删除奇数,就像这样: def remove_odd(l): for i in l: if i % 2 != 0: l.remove(i) print l return l re ...

最新文章

  1. Spring Boot 太重,Vert.x 真香!
  2. 数据挖掘流程(三):特征工程
  3. 2019最全Python入门学习路线,绝对是最全
  4. Matlab错误:Y must be a vector or a character array
  5. PyODPS DataFrame 的代码在哪里跑
  6. 查询进程打开的文件(转)
  7. golang odbc mysql_go语言通过odbc操作Access数据库的方法
  8. iphone全部机型_iPhone 上新,首次明确支持中国北斗
  9. 1句Log引发的悲剧
  10. onItemLongClick+onCreateContextMenu实现长按ListItem弹出不同菜单
  11. 分享24款非常有用的 jQuery 插件
  12. python实现常见的整数进制、字符进制、ASCII码进制之间的转换
  13. jdk8 32位_HashMap源码分析 jdk8
  14. 浪潮 服务器数据安全管理系统,浪潮SSC运维安全管控系统
  15. python空字典怎么定义_python dict怎么定义
  16. 电力电子技术(16)——直流斩波电路
  17. 超全的matlab绘图实例及代码(曲线,曲面,饼状图,柱形图,网格图,球面等)
  18. 18336万!华为中标昌平区政务云2022年-2025年服务项目
  19. 研究生论文致谢怎么写?
  20. 总结2012年世界经济形势主要特征

热门文章

  1. OSChina 周五乱弹 —— 为什么程序媛那么少?
  2. 潜在数学中 放眼数学外——记中国科学院院士、复旦大学教授李大潜zz
  3. mscorsvw.exe是什么
  4. JavaScript学习笔记(四) ES6
  5. Linux各运行级别含义,描述Linux运行级别0-6的各自含义
  6. 打游戏买什么手机好?rog3性能强 网速稳!
  7. 奥布莱恩杯尘埃落定 人工智能立功了!
  8. X(解释变量)增加一单位标准差,Y(被解释变量)增加多少标准差?
  9. 一天一个 Linux 命令(27):mkfs 命令
  10. PM2部署React项目(Ubuntu服务器)