繁体   English   中英

python:使用列表理解过滤嵌套的字符串列表

[英]python: filter a nested list of strings with list comprehension

我正在尝试使用列表理解来过滤此列表:

inp = [['Fire 46.0.1', 'vlc 2.2.3','opt 0.9.9.10'],
       ['opt 0.9.9.11', 'notepad 6.9', 'adobe. 6.9', 'vlc 2.3.3']]

进入

out = [['Fire 46.0.1', 'vlc 2.2.3',],
       ['notepad 6.9', 'adobe. 6.9', 'vlc 2.3.3']]

即删除所有以“ opt”开头的字符串。 我正在尝试类似的东西

[soft for ls in inp if soft not soft.startswith('opt')]

但是语法有问题。 有什么帮助吗?

应该这样做:

[[s for s in item if not s.startswith('opt')] for item in inp ]

使用startswith筛选出的项目与opt从子列表

您需要使用嵌套列表理解功能来过滤嵌套列表。

[[y for y in x if not y.startswith('opt')] for x in inp]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM