Linking Sets and Dictionaries

时间:2016-10-20 19:30:01

标签: python python-3.x

I am working on a project in Python which requires lookup of set values in a dictionary.

I have two dictionaries, both with ID as the key. One dictionary has names as the values, and the other has emails as the values.

For example:

Names Dictionary:

{'cbp750': 'Crystle Purifoy', 'mis918': 'Marquita See', 'bmb865': 'Bok 
 Bobbitt', 'dch273': 'Danny Halverson', 'etf890': 'Elvia Fortin', 'nve360':
 'Nakisha Ehrmann'}


Emails Dictionary:

{'cbp750': 'c.purifoy@utexas.edu', 'mis918': 'm.see@utexas.edu', 'bmb865':
 'b.bobbitt@utexas.edu', 'dch273': 'd.halverson@utexas.edu', 'etf890':
 'e.fortin@utexas.edu', 'nve360':'n.ehrmann@utexas.edu'}

If I have a set of values, such as: {'mis918', 'etf890', 'nve360'}, how do I look up these set values in the dictionaries to return the name and email in the format Name(Email) such that looking up mis918 would return Marquita See (M.See@utexas.edu)?

My current attempts to use for loops have failed, so I have no idea where I should start. Help is greatly appreciated.

4 个答案:

答案 0 :(得分:1)

I'm not sure this is what you're looking for, but you can just access a dictionary value by passing in the key:

names = {
    'cbp750': 'Crystle Purifoy',
    'mis918': 'Marquita See',
    'bmb865': 'Bok Bobbitt',
    'dch273': 'Danny Halverson',
    'etf890': 'Elvia Fortin',
    'nve360': 'Nakisha Ehrmann'
}

emails = {
    'cbp750': 'c.purifoy@utexas.edu',
    'mis918': 'm.see@utexas.edu',
    'bmb865': 'b.bobbitt@utexas.edu',
    'dch273': 'd.halverson@utexas.edu',
    'etf890': 'e.fortin@utexas.edu',
    'nve360':'n.ehrmann@utexas.edu'
}


set_of_ids =  {'mis918', 'etf890', 'nve360'}

for _id in set_of_ids:
    if _id in names and _id in emails:
        print('{} ({})'.format(names[_id], emails[_id]))

答案 1 :(得分:1)

In order to access an item from the dict, you can pass key as dict[key] to access it's value. In your case, key is the item in set. Iterate over your set and use the item as key for fetching the values from email_dict and name_dict.

Note: In case there is possibility that the item in the set may not be the key in dict, use dict.get(key, '') instead. It will return the value as '' empty string for unknown keys. For example:

>>> my_dict = {'a': 2}
>>> my_dict.get('a', '')  # Returns value as 'a' is key in 'my_dict'
2 
>>> my_dict.get('b', '')  # Return empty string as 'b' is not key in 'my_dict'
''

Below is the sample code for your issue (assuming all item in set are present in your both dict):

my_set = {'mis918', 'etf890', 'nve360'}
for item in my_set:
    print('Set Value: ', item, ' ; Email: ', email_dict[item], ' ; Name: ', name_dict[item])

# Output of above code 
Set Value:  etf890  ; Email:  e.fortin@utexas.edu  ; Name:  Elvia Fortin
Set Value:  mis918  ; Email:  m.see@utexas.edu  ; Name:  Marquita See
Set Value:  nve360  ; Email:  n.ehrmann@utexas.edu  ; Name:  Nakisha Ehrmann

where email_dict and name_dict are dict as are mentioned in question, i.e:

name_dict = {
        'cbp750': 'Crystle Purifoy', 
        'mis918': 'Marquita See', 
        'bmb865': 'BokBobbitt', 
        'dch273': 'Danny Halverson', 
        'etf890': 'Elvia Fortin', 
        'nve360': 'Nakisha Ehrmann'
    }

email_dict = {
        'cbp750': 'c.purifoy@utexas.edu', 
        'mis918': 'm.see@utexas.edu', 
        'bmb865': 'b.bobbitt@utexas.edu', 
        'dch273': 'd.halverson@utexas.edu', 
        'etf890': 'e.fortin@utexas.edu',                  
        'nve360':'n.ehrmann@utexas.edu'
   }

答案 2 :(得分:0)

A short one liner answer would be:

# names -> name dictionary, emails -> email dictionary, idSet -> contains ids to be searched.

['{} ({})'.format(names[idx], emails[idx]) for idx in idSet if idx in names and idx in emails]

答案 3 :(得分:0)

Use the following approach to get the expected result (using re.sub function to format local part of email):

import re

names = {'cbp750': 'Crystle Purifoy', 'mis918': 'Marquita See', 'bmb865': 'Bok  Bobbitt', 'dch273': 'Danny Halverson', 'etf890': 'Elvia Fortin', 'nve360': 'Nakisha Ehrmann'}

emails = {'cbp750': 'c.purifoy@utexas.edu', 'mis918': 'm.see@utexas.edu',\
          'bmb865': 'b.bobbitt@utexas.edu', 'dch273': 'd.halverson@utexas.edu',\
          'etf890': 'e.fortin@utexas.edu', 'nve360':'n.ehrmann@utexas.edu'}

aliases = {'mis918', 'etf890', 'nve360'}

groups = [
    names[a]+' ('+ re.sub(r'^(\w)\.(\w+)(?=@)', lambda m: m.group(1).upper() + '.' + m.group(2).capitalize(), emails[a]) +')'
    for a in aliases if a in names and a in emails
    ]

print(groups)
# ['Marquita See (M.See@utexas.edu)', 'Elvia Fortin (E.Fortin@utexas.edu)', 'Nakisha Ehrmann (N.Ehrmann@utexas.edu)']