开发者

python how to search an item in a nested list

开发者 https://www.devze.com 2023-03-25 09:46 出处:网络
say I have this list: li = [[\"0\", \"20\", \"ar\"], [\"20\", \"40\", \"asdasd\"], [\"50\", \"199\", \"bar\"], [\"24\", \"69\", \"sarkozy\"]]

say I have this list:

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

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 = [[开发者_如何学Go"50", "199", "bar"], ["24", "69", "sarkozy"]]

How can I obtain this list?


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

精彩评论

暂无评论...
验证码 换一张
取 消