gpt4 book ai didi

python - 如果某些多个关键字匹配则过滤字符串

转载 作者:行者123 更新时间:2023-12-01 08:38:05 24 4
gpt4 key购买 nike

我是Python新手,所以如果某些单词在该行中匹配,那么从文件中提取行对我来说是一个挑战。

我有一个 html 链接数组,例如:我的输入

['https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/03/trackr-rebrands-to-adero-pivots-to-finding-whereabouts-of-groups-of-items/&ct=ga&cd=caeyacotntqymzy0njawnzu2mji3otq0mziazjhmndaxowrjnmviywm4otpjb206zw46vvm&usg=afqjcnebtnj9ybuywkwcp33xlzvkdtqndq', 'https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/03/will-uber-gobble-up-lime-or-fly-off-with-bird/&ct=ga&cd=caeyasotntqymzy0njawnzu2mji3otq0mziazjhmndaxowrjnmviywm4otpjb206zw46vvm&usg=afqjcnf4upl3v1gzd5a1xr0pgpvc1zedya', 'https://www.google.com/alerts/remove?source=alertsmail&hl=en&gl=us&msgid=ntqymzy0njawnzu2mji3otq0mw&s=ab2xq4hy_egw7prfejiq3uhjazt-7cjtjoilna0', 'https://www.google.com/alerts?source=alertsmail&hl=en&gl=us&msgid=ntqymzy0njawnzu2mji3otq0mw', 'https://www.google.com/alerts?source=alertsmail&hl=en&gl=us&msgid=ntqymzy0njawnzu2mji3otq0mw', 'https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/03/will-uber-gobble-up-lime-or-fly-off-with-bird/&ct=ga&cd=caeyacoumtq0mjmwnzuwmtg3odi4ndq5mtmygjbimdy5nmi3nmjkmwuymdq6y29tomvuolvt&usg=afqjcnf4upl3v1gzd5a1xr0pgpvc1zedya', 'https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/04/hulu-to-top-23-million-subscribers-by-year-end/&ct=ga&cd=caeyasoumtq0mjmwnzuwmtg3odi4ndq5mtmygjbimdy5nmi3nmjkmwuymdq6y29tomvuolvt&usg=afqjcnfyn98cfz1e8oyay72qwdchsg_f_q', 'https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/03/fintech-investors-and-founders-to-judge-startup-battlefield-africa/&ct=ga&cd=caeyaioumtq0mjmwnzuwmtg3odi4ndq5mtmygjbimdy5nmi3nmjkmwuymdq6y29tomvuolvt&usg=afqjcnfxredq8rapscoupmhdzbf-husqyw', 'https://www.google.com/alerts/remove?source=alertsmail&hl=en&gl=us&msgid=mtq0mjmwnzuwmtg3odi4ndq5mtm&s=ab2xq4hjxw0sqeep2yq6odjmq700btmzyqs3svy', 'https://www.google.com/alerts?source=alertsmail&hl=en&gl=us&msgid=mtq0mjmwnzuwmtg3odi4ndq5mtm', 'https://www.google.com/alerts?source=alertsmail&hl=en&gl=us&msgid=mtq0mjmwnzuwmtg3odi4ndq5mtm', 'https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/04/podcast-series-c/&ct=ga&cd=caeyacoumtm0mjyxmzezntg2oti0nju0odgygjg0n2i3ogq3nmi1owu1yjk6y29tomvuolvt&usg=afqjcngnd6o3mwrmbj-uc-1a84mlixp26w', 'https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/03/agricool-raises-another-28-million-to-grow-fruits-in-containers/&ct=ga&cd=caeyasoumtm0mjyxmzezntg2oti0nju0odgygjg0n2i3ogq3nmi1owu1yjk6y29tomvuolvt&usg=afqjcnecc5wtp2klzwob021zzcxodrkstg', 'https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/04/fivetran-announces-15m-series-a-to-build-automated-data-pipelines/&ct=ga&cd=caeyaioumtm0mjyxmzezntg2oti0nju0odgygjg0n2i3ogq3nmi1owu1yjk6y29tomvuolvt&usg=afqjcnfvplce8-juoffflxwe8-ttrqaz_g', 'https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/04/bios-health/&ct=ga&cd=caeyayoumtm0mjyxmzezntg2oti0nju0odgygjg0n2i3ogq3nmi1owu1yjk6y29tomvuolvt&usg=afqjcngjpzu9t9hyjfjkaf1sefloujvjhq', 'https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/04/freeletics-raises-45m-for-its-ai-powered-fitness-coach/&ct=ga&cd=caeybcoumtm0mjyxmzezntg2oti0nju0odgygjg0n2i3ogq3nmi1owu1yjk6y29tomvuolvt&usg=afqjcneh-xmavwlbin0hfswkmrbniousnw', 'https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/04/mixcloud-select/&ct=ga&cd=caeybsoumtm0mjyxmzezntg2oti0nju0odgygjg0n2i3ogq3nmi1owu1yjk6y29tomvuolvt&usg=afqjcneh_kjqkido1dz30dgax2cv1-6g6w', 'https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/04/atomicos-fourth-state-of-the-european-tech-report-highlights-lots-of-rosy-numbers-but-also-a-discrimination-problem/&ct=ga&cd=caeybioumtm0mjyxmzezntg2oti0nju0odgygjg0n2i3ogq3nmi1owu1yjk6y29tomvuolvt&usg=afqjcnfcgwt_rwsya4ulxw6im7mcy0a74q', 'https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/04/fortressiq-raises-12m-to-bring-new-ai-twist-to-process-automation/&ct=ga&cd=caeybyoumtm0mjyxmzezntg2oti0nju0odgygjg0n2i3ogq3nmi1owu1yjk6y29tomvuolvt&usg=afqjcngn2mdhvdzsxhjpy9wako015rsd9w', 'https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/04/birth-control-delivery-startup-nurx-now-offers-an-at-home-hpv-testing-kit/&ct=ga&cd=caeyccoumtm0mjyxmzezntg2oti0nju0odgygjg0n2i3ogq3nmi1owu1yjk6y29tomvuolvt&usg=afqjcnhufwynz2xvx8h7y5njesu5umrqbw', 'https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/04/faraday-future-furloughs-more-employees-as-cash-woes-continue/&ct=ga&cd=caeycsoumtm0mjyxmzezntg2oti0nju0odgygjg0n2i3ogq3nmi1owu1yjk6y29tomvuolvt&usg=afqjcng7ma6lr8xqakdvwbcr9kmgkvhvnw', 'https://www.google.com/alerts/remove?source=alertsmail&hl=en&gl=us&msgid=mtm0mjyxmzezntg2oti0nju0odg&s=ab2xq4j8dtcluvhhgyayaorwyeut2bkvyp4mrac', 'https://www.google.com/alerts?source=alertsmail&hl=en&gl=us&msgid=mtm0mjyxmzezntg2oti0nju0odg', 'https://www.google.com/alerts?source=alertsmail&hl=en&gl=us&msgid=mtm0mjyxmzezntg2oti0nju0odg']

我的预期输出应该是

['https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/03/agricool-raises-another-28-million-to-grow-fruits-in-containers/&ct=ga&cd=caeyasoumtm0mjyxmzezntg2oti0nju0odgygjg0n2i3ogq3nmi1owu1yjk6y29tomvuolvt&usg=afqjcnecc5wtp2klzwob021zzcxodrkstg','https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/04/freeletics-raises-45m-for-its-ai-powered-fitness-coach/&ct=ga&cd=caeybcoumtm0mjyxmzezntg2oti0nju0odgygjg0n2i3ogq3nmi1owu1yjk6y29tomvuolvt&usg=afqjcneh-xmavwlbin0hfswkmrbniousnw','https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/04/fortressiq-raises-12m-to-bring-new-ai-twist-to-process-automation/&ct=ga&cd=caeybyoumtm0mjyxmzezntg2oti0nju0odgygjg0n2i3ogq3nmi1owu1yjk6y29tomvuolvt&usg=afqjcngn2mdhvdzsxhjpy9wako015rsd9w']

我想过滤包含特定关键字的链接,例如“任何数字0-9”、“百万”、“加薪”、“资金”、“估值”等。

我浏览了 stackoverflow 中的许多链接,但找不到我要找的内容。任何帮助将不胜感激。

最佳答案

你可以尝试:

candidates = ['https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/03/corporate-food-catering-startup-chewse-raises-19-million/&ct=ga&cd=caeyacoumtqxotu0mdi1mjkxndk4otc1mteygjg0n2i3ogq3nmi1owu1yjk6y29tomvuolvt&usg=afqjcngcalj2l2089xqyzdr5clovuuvafq', 'https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/03/sleep-tracking-ring-oura-raises-20-million-from-michael-dell-lance-armstrong-and-others/&ct=ga&cd=caeyasoumtqxotu0mdi1mjkxndk4otc1mteygjg0n2i3ogq3nmi1owu1yjk6y29tomvuolvt&usg=afqjcng8wdz35c5krnjcnypbw21b0pihfg', 'https://www.google.com/alerts/remove?source=alertsmail&hl=en&gl=us&msgid=mtqxotu0mdi1mjkxndk4otc1mte&s=ab2xq4j8dtcluvhhgyayaorwyeut2bkvyp4mrac', 'https://www.google.com/alerts?source=alertsmail&hl=en&gl=us&msgid=mtqxotu0mdi1mjkxndk4otc1mte', 'https://www.google.com/alerts?source=alertsmail&hl=en&gl=us&msgid=mtqxotu0mdi1mjkxndk4otc1mte']

def interesting(text):
text = text.lower()
if any([word in text for word in ['billions', 'funding', 'valuations'] + ['%dm' % i for i in range(10)]]):
return True

# Add other conditions

return False

result = list(filter(interesting, candidates))
print(result)

此示例的输出是:

['https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/03/corporate-food-catering-startup-chewse-raises-19-million/&ct=ga&cd=caeyacoumtqxotu0mdi1mjkxndk4otc1mteygjg0n2i3ogq3nmi1owu1yjk6y29tomvuolvt&usg=afqjcngcalj2l2089xqyzdr5clovuuvafq', 'https://www.google.com/url?rct=j&sa=t&url=https://techcrunch.com/2018/12/03/sleep-tracking-ring-oura-raises-20-million-from-michael-dell-lance-armstrong-and-others/&ct=ga&cd=caeyasoumtqxotu0mdi1mjkxndk4otc1mteygjg0n2i3ogq3nmi1owu1yjk6y29tomvuolvt&usg=afqjcng8wdz35c5krnjcnypbw21b0pihfg', 'https://www.google.com/alerts/remove?source=alertsmail&hl=en&gl=us&msgid=mtqxotu0mdi1mjkxndk4otc1mte&s=ab2xq4j8dtcluvhhgyayaorwyeut2bkvyp4mrac', 'https://www.google.com/alerts?source=alertsmail&hl=en&gl=us&msgid=mtqxotu0mdi1mjkxndk4otc1mte', 'https://www.google.com/alerts?source=alertsmail&hl=en&gl=us&msgid=mtqxotu0mdi1mjkxndk4otc1mte']

关于python - 如果某些多个关键字匹配则过滤字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53626306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com