简体   繁体   中英

Find a list of substring into a list of strings with python

I have a list of links and want to select some them based on list of subtrings into the link. I'd like to select the links with the substrings medalha or mencao-honrosa . Following a sample of selected links.

https://onciencias.org/resultado/detalhe/9-ano-ensino-fundamental/medalha-ouro
https://onciencias.org/resultado/detalhe/9-ano-ensino-fundamental/medalha-prata
https://onciencias.org/resultado/detalhe/9-ano-ensino-fundamental/medalha-bronze
https://onciencias.org/resultado/detalhe/9-ano-ensino-fundamental/mencao-honrosa
https://onciencias.org/resultado/detalhe/1-serie/medalha-ouro
https://onciencias.org/resultado/detalhe/1-serie/medalha-prata
https://onciencias.org/resultado/detalhe/1-serie/medalha-bronze
https://onciencias.org/resultado/detalhe/1-serie/mencao-honrosa
https://onciencias.org/resultado/detalhe/2-serie/medalha-ouro
https://onciencias.org/resultado/detalhe/2-serie/medalha-prata
https://onciencias.org/resultado/detalhe/2-serie/medalha-bronze
https://onciencias.org/resultado/detalhe/2-serie/mencao-honrosa
https://onciencias.org/resultado/detalhe/3-serie/medalha-ouro
https://onciencias.org/resultado/detalhe/3-serie/medalha-prata
https://onciencias.org/resultado/detalhe/3-serie/medalha-bronze
https://onciencias.org/resultado/detalhe/3-serie/mencao-honrosa
https://onciencias.org/resultado/detalhe/4-ano-tecnico/medalha-ouro
https://onciencias.org/resultado/detalhe/4-ano-tecnico/medalha-prata
https://onciencias.org/resultado/detalhe/4-ano-tecnico/medalha-bronze
https://onciencias.org/resultado/detalhe/4-ano-tecnico/mencao-honrosa

I did the following code and it's working. What's best another way to write this code and avoid to use or to separate the substrings.

for temp_link in list_links:
    if(str(temp_link).find('medalha') != -1 or str(temp_link).find('mencao-honrosa') != -1):
        links.append(url_home+temp_link[1:])
        print(url_home+temp_link[1:])

Thank you.

Try this,

[url for url in urls if 'medalha' in url or 'mencao-honrosa' in url]

or if you have a list of search terms, maybe something like this.

search_terms = ['medalha', 'mencao-honrosa']
[url for url in urls if any([term in url for term in search_terms])]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM