python如何在嵌套列表中搜索项目 [英] python how to search an item in a nested list

查看:420
本文介绍了python如何在嵌套列表中搜索项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有这个清单:

li = [["0", "20", "ar"], ["20", "40", "asdasd"], ["50", "199", "bar"], ["24", "69", "sarkozy"]]

现在,忘记数字了,它们让我认识到字符串的位置.因此,基本上,鉴于我手头有字符串"ar",如何提取所有包含"ar"的列表?

Now, forget about the numbers, they are something that let me recognize the position of string. So basically, given that I have the string "ar" in hand, how can I extract all the lists that contain "ar"?

new_li = [["50", "199", "bar"], ["24", "69", "sarkozy"]]

如何获取此列表?

推荐答案

>>> [x for x in li if 'ar' in x[2]]
[['0', '20', 'ar'], ['50', '199', 'bar'], ['24', '69', 'sarkozy']]

这篇关于python如何在嵌套列表中搜索项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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