python - 如何从短语列表中查找字典中的短语并使用找到和计数的短语创建数据框。应计算重复项
问题描述
phrases = ['i am good', 'going to the market', 'eating cookies']
dictionary = {'http://www.firsturl.com': 'i am going to the market and tomorrow will be eating cookies',
'http://www.secondurl.com': 'tomorrow is my birthday and i shall be',
'http://www.thirdurl.com': 'i am good and will go to sleep'}
如果至少有一个匹配:预期输出:
url phrasecount phrase
http://www.firsturl.com 2 going to the market, eating cookies
http://www.thirdurl.com 1 i am good
如果所有 3 个 url 均不匹配,则仅返回第一个出现的 url,计数为零且预期输出为空白短语:
url phrasecount phrase
http://www.firsturl.com 0
解决方案
df
从相应的设置初始数据帧dictionary
:
df = pd.DataFrame({'urls': list(dictionary.keys()), 'strings': list(dictionary.values())})
pattern = '|'.join(phrases)
处理数据框:
s = df.pop('strings').str.findall(pattern)
df = df.assign(phrasecount=s.str.len(), phrase=s.map(', '.join))
df = df.drop_duplicates(subset='phrasecount') if df['phrasecount'].eq(0).all() else df[df['phrasecount'].ne(0)]
结果:
# print(df)
urls phrasecount phrase
0 http://www.firsturl.com 2 going to the market, eating cookies
2 http://www.thirdurl.com 1 i am good
推荐阅读
- pine-script - 回测 alertcondition()
- python - 帐户创建日期和当前日期之间的Python差异
- reverse-engineering - 诺基亚 215 4G 固件修改后如何修改校验和?
- python - 使用 librosa/TensorFlow 在服务器 (python) 和客户端 (javascript) 上创建相同的 mel-spectrogram
- python - 如何使用 selenium 性能日志来获取在新标签页中打开的所有网站的流量?
- python - 为现有 Teams 聊天机器人 (python) 创建 https 端点的最有效方法是什么?
- postman - 通过 IAP 身份验证是否需要有效的 OAuth 2.0 令牌?
- python - 如何在反引号的文本中使用 sphinx 指令?
- nginx - 无法在 Nginx 上阻止来自 3g 的 IP
- c# - Formsauthentication 在从 Visual Studio 启动应用程序时未显示登录页面