Warm tip: This article is reproduced from serverfault.com, please click

create a dict with first name as key, full name associated with the key as value

发布于 2018-12-21 16:59:14

I have a list:

name_list = ["David Joyner", "David Zuber", "Brenton Joyner",
             "Brenton Zuber", "Nicol Barthel", "Shelba Barthel",
             "Shelba Crowley", "Shelba Fernald", "Shelba Odle",
             "Shelba Fry", "Maren Fry"]

I want to write a function to return a dictionary which has first name as key, all full names associated with the first name as value:

 {'Shelba': ['Shelba Barthel', 'Shelba Crowley', 'Shelba Fernald', 
 'Shelba Odle', 'Shelba Fry'], 'David': ['David Joyner', 'David Zuber'], 
 'Brenton': ['Brenton Joyner', 'Brenton Zuber'], 'Maren': ['Maren Fry'], 
 'Nicol': ['Nicol Barthel']}

thanks.

Questioner
potentialwjy
Viewed
0
Patrick Artner 2018-12-22 01:25:49

Smart way (1 pass through your list):

from collections import defaultdict

name_list = ["David Joyner", "David Zuber", "Brenton Joyner",
             "Brenton Zuber", "Nicol Barthel", "Shelba Barthel",
             "Shelba Crowley", "Shelba Fernald", "Shelba Odle",
             "Shelba Fry", "Maren Fry"]

rv = defaultdict(list)
for elem in name_list:
    rv[elem.split()[0]].append(elem) 

print(dict(rv))

Output:

{'David':  ['David Joyner', 'David Zuber'], 
 'Brenton':['Brenton Joyner', 'Brenton Zuber'], 
 'Nicol':  ['Nicol Barthel'], 
 'Shelba': ['Shelba Barthel','Shelba Crowley','Shelba Fernald','Shelba Odle','Shelba Fry'], 
 'Maren':  ['Maren Fry']}

Slow way (might look nice, but is inefficient):

d = {key:[n for n in name_list if n.startswith(key)] 
          for key in set( p.split()[0] for p in name_list)}

This is computationally far worse then the defaultdict version because it traverses the list multiple times:

  • once to create set( p.split()[0] for p in name_list)
  • and for every name we just gathered it traverses the full list again to create the dict-value [n for n in name_list if n.startswith(key)]

If you need to sort afterwards, simply use the list.sort() on your dicts values:

# sorting the data afterwards
for k in rv:
    rv[k].sort(key = lambda x:x.split()[1]) # to sort by last names

Output:

# only difference when sorted: see key `Shelba`
{'David':  ['David Joyner', 'David Zuber'], 
 'Brenton':['Brenton Joyner', 'Brenton Zuber'], 
 'Nicol':  ['Nicol Barthel'], 
 'Shelba': ['Shelba Barthel','Shelba Crowley','Shelba Fernald','Shelba Fry','Shelba Odle'], 
 'Maren':  ['Maren Fry']}