创建一个以名字为键的字典,将与该键关联的全名作为值

时间:2018-12-21 16:59:15

标签: python python-3.x dictionary

我有一个列表:

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

我想编写一个函数来返回以名字为键的字典,所有与名字相关的全名作为值:

 {'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']}

谢谢。

6 个答案:

答案 0 :(得分:2)

智能方式(1条通过您的列表):

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

输出:

{'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']}

慢速(可能看起来不错,但效率低下):

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

在计算上,它比defaultdict版本差很多,因为它多次遍历列表:

  • 一次创建set( p.split()[0] for p in name_list)
  • 对于我们刚刚收集的每个名称,它再次遍历完整列表以创建字典值[n for n in name_list if n.startswith(key)]

如果以后需要排序,只需在list.sort()的值上使用dict

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

输出:

# 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']}

答案 1 :(得分:0)

如Patrick Artner在评论中所建议,请使用import numpy as np import pandas as pd from PyQt5 import QtCore, QtGui, QtWidgets def pandas_to_str(): df = pd.DataFrame({ 'A' : 1., 'B' : pd.Timestamp('20130102'), 'C' : pd.Series(1,index=list(range(4)),dtype='float32'), 'D' : np.array([3] * 4,dtype='int32'), 'E' : pd.Categorical(["test","train","test","train"]), 'F' : 'foo' }) return df.to_string(col_space =14,justify = "justify") if __name__ == '__main__': import sys app = QtWidgets.QApplication(sys.argv) w = QtWidgets.QTextBrowser() w.setStyleSheet('color: blue') w.setFont(QtGui.QFont("Monospace")) w.setWordWrapMode(QtGui.QTextOption.NoWrap) w.setText(pandas_to_str()) w.showMaximized() sys.exit(app.exec_()) 。怎么样:

defaultdict

答案 2 :(得分:0)

尝试一下:

def build_dict(name_list)
result_dict = {}
for name in name_list:
    names = name.split(" ")
    if names[0] in result_dict:
        result_dict[names[0]].append(names[1])
    else:
        result_dict[names[0]] = [names[1]]

return result_dict

这个想法是迭代名称列表,将每个名称分为名字和姓氏,看看名字是否在字典中。如果是这样,请查找它,并将姓氏添加到姓氏列表中。如果不是,请为其分配一个列表,该列表的唯一成员是姓氏

答案 3 :(得分:0)

def name_lists(name_list):
    d = {}
    
    for name in name_list:
                
        first_name = name.split()[0]
        if first_name in d:
            
            d[first_name].append(name)
            d[first_name].sort()
            
        else:
            d[first_name]=[name]

    return d

答案 4 :(得分:0)

def name_counts(name_list):
    result_dict = {}
    for name in name_list:
        names = name.split(" ")
        if names[0] in result_dict:
            result_dict[names[0]] += 1
        else:
            result_dict[names[0]] = 1
    return result_dict 

答案 5 :(得分:0)

def name_lists(n_list):
    result_dict = {}
    n_list.sort()
    for name in n_list:
        names = name.split(" ")
        if names[0] in result_dict:
            result_dict[names[0]].append(name)
        else:
            result_dict[names[0]] = [name]
    return result_dict