Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9005205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:04:56+00:00 2026-06-16T01:04:56+00:00

I have (what seems to me is) a pretty convoluted problem. I’m going to

  • 0

I have (what seems to me is) a pretty convoluted problem. I’m going to try to be as succinct as possible – though in order to understand the issue fully, you might have to click on my profile and look at the (only other) two questions I’ve posted on StackOverflow. In short: I have two lists — one is comprised of email strings that contain a facility name, and a date of incident. The other is comprised of the facility ids for each email (I use one of the following regex functions to get this list). I’ve used Regex to be able to search each string for these pieces of information. The 3 Regex functions are:

def find_facility_name(incident):

    pattern = re.compile(r'Subject:.*?for\s(.+?)\n')
    findPat1 = re.search(pattern, incident)
    facility_name = findPat1.group(1)

    return facility_name



def find_date_of_incident(incident):


    pattern = re.compile(r'Date of Incident:\s(.+?)\n')
    findPat2 = re.search(pattern, incident)
    incident_date = findPat2.group(1)

    return incident_date

def find_facility_id(incident):
    pattern = re.compile('(\d{3})\n')
    findPat3 = re.search(pattern, incident)
    f_id = findPat3.group(1)

    return f_id

I also have a dictionary that is formatted like this:

d = {'001' : 'Facility #1', '002' : 'Another Facility'...etc.}

I’m trying to COMBINE the two lists and sort by the Key values in the dictionary, followed by the Date of Incident. Since the key values are attached to the facility name, this should automatically caused emails from the same facilities to be grouped together. In order to do that, I’ve tried to use these two functions:

def get_facility_ids(incident_list):
'''(lst) -> lst

Return a new list from incident_list that inserts the facility IDs from the
get_facilities dictionary into each incident.

'''
f_id = []
for incident in incident_list:
    find_facility_name(incident)
    for k in d:
        if find_facility_name(incident) == d[k]:
            f_id.append(k)

return f_id

id_list = get_facility_ids(incident_list)

def combine_lists(L1, L2):
    combo_list = []
    for i in range(len(L1)):
        combo_list.append(L1[i] + L2[i])

return combo_list

combination = combine_lists(id_list, incident_list)

def get_sort_key(incident):
'''(str) -> tup

Return a tuple from incident containing the facility id as the first
value and the date of the incident as the second value.

'''

return (find_facility_id(incident), find_date_of_incident(incident))

final_list = sorted(combination, key=get_sort_key)

Here is an example of what my input might be and the desired output:

d = {'001' : 'Facility #1', '002' : 'Another Facility'...etc.}
input: first_list = ['email_1', 'email_2', etc.]
first output: next_list = ['facility_id_for_1+email_1', 'facility_id_for_2 + email_2', etc.]
DESIRED OUTPUT: FINAL_LIST = sorted(next_list, key=facility_id, date of incident)

The only problem is, the key values are not matching properly with what’s found in each individual email string. Some DO, others are completely random. I have no idea why this is happening, but I have a feeling it has something to do with the way I’m combining the two lists. Can anyone help this lowly n00b? Thanks!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:04:57+00:00Added an answer on June 16, 2026 at 1:04 am

    First off, I would suggest reversing your ID-to-name dictionary. Looking up a value by key is very fast but finding a key by value is very slow.

    rd = { name: id_num for id_num, name in d.items() }
    

    Then your first function can be replaced by a list comprehension:

    id_list = [rd[find_facility_name(incident)] for incident in incident_list]
    

    This might also expose why you’re getting messed up values in your results. If an incident has a facility name that’s not in your dictionary, this code will raise a KeyError (whereas your old function would just skip it).

    Your combine function is very similar to Python’s built in zip function. I’d replace it with:

    combination = [id+incident for id, incident in zip(id_list, incident_list)]
    

    However, since you’re building the first list from the second one, it might make sense to build the combined version directly, rather than making separate lists and then combining them in a separate step. Here’s an update to the list comprehension above that goes right to the combination result:

    combination = [rd[find_facility_name(incident)] + incident
                   for incident in incident_list]
    

    To do the sort, you can use the ID string that we just prepended to the email message, rather than parsing to find the ID again:

    combination.sort(key=lambda x: (x[0:3], get_date_of_incident(x)))
    

    The 3 in the slice is based off of your example of "001" and "002" as the id values. If the actual ids are longer or shorter you’ll need to adjust that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The question seems pretty well formulated I have a virtual machine which implements only
I have a pretty basic problem but I can't seem to figure it out...
I have been researching this issue pretty extensively and cannot seem to find an
I have what seems like a simple problem. I have a Spring web app,
It seems pretty common to me to have an argument, in a dynamically typed
Seems pretty simple but I can't get it to work. I have two divs
Seems pretty simple, but then nothing is. I have two values: t.integer quantity t.decimal
I have started using RestSharp to call an webapi proejct as it seems pretty
im new to backbone js and my first interaction seems pretty comple. the problem
I have a validate method which seems pretty simple because I use something very

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.