Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checking 2 lists
#1
Hi Guys,

I have 2 lists, one is:

def allowed_email_links():
    """ this function keeps a list email structures we are allowed to click ... """
    return ["registration.activate",
            "/activate/"]
Which returns strings, these strings must be somewhere in a url for us to save them like:

            email_extract_urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', response[0][1].decode())

            if len(email_extract_urls) > 0:
                if allowed_email_links() in email_extract_urls:
                    email_container.append(email_extract_urls)
What i'm trying to do is, if any of the urls in the email_extract_urls list contains a string from the allowed_email_links() list then we will add it to the email_container

But the way i have it above it's looking for the string alone, rather than if it contains the string, is there a way to do this?

cheers guys

Graham
Reply
#2
You generally do this with a loop:

email_container = []
allowed_links = allowed_email_links()
for extracted_url in email_extract_urls:
    for allowed_link in allowed_links:
        if allowed_link in extracted_url:
            email_container.append(extracted_url)
            break
You check each extracted url (outer for loop) against each allowed link (inner for loop). When you find a match, you include it and break to avoid including it more than once.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
If the order is not important:

allowed_links = set(email_extract_urls) & set(allowed_email_links())
A set has only unique elements.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,190 Mar-20-2019, 08:01 PM
Last Post: stillsen
  checking for matches between lists henrik0706 2 2,645 Oct-24-2017, 11:08 AM
Last Post: henrik0706

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020