How to Extract List Items Matching Multiple Keywords in Python
This article walks through a follower's Python question about filtering nested list titles for both "python" and "应用" keywords, presenting two clear solutions—regular list access and the operator module—complete with code snippets and result screenshots.
Introduction
In response to a follower's question about extracting list elements that contain both "python" and "应用" (application) keywords, the author demonstrates two Python solutions.
Method 1: Regular List Access
# -*- coding: utf-8 -*-
# Method 1
keywordlist = ['python', '应用']
title = [['人生苦短,我应用python'], ['Rick Xiang 666'], ['歪歪nb'], ['为才哥打call'], ['网络爬虫的应用']]
for luwen in title:
if keywordlist[0] in luwen[0] and keywordlist[1] in luwen[0]:
print(luwen)The code iterates over the nested list, checks both keywords, and prints the matching sub‑list.
Method 2: Using the operator Module
# -*- coding: utf-8 -*-
import operator
keywordlist = ['python', '应用']
title = [['人生苦短,我应用python'], ['Rick Xiang 666'], ['歪歪nb'], ['为才哥打call'], ['网络爬虫的应用'], ['python爬虫与数据挖掘']]
for luwen in title:
if operator.contains(luwen[0], keywordlist[0]) and operator.contains(luwen[0], keywordlist[1]):
print(luwen)This approach uses operator.contains to perform the same check.
Conclusion
The article shows two straightforward ways to filter a list of strings by multiple keywords, helping the asker solve the problem and encouraging readers to explore additional methods.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
