Combining two lists and sorting by reference to the Python dictionary

advertisements

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!!!


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.