如何使用两个列表制作字典

时间:2017-11-17 18:31:08

标签: python list dictionary

所以我有这个代码,我正在尝试创建firstnames字典的lastnames。我在原始词典中创建了两个列表:

person_to_friends = {
    'Jay Pritchett': ['Claire Dunphy', 'Gloria Pritchett', 'Manny Delgado'], 
    'Claire Dunphy': ['Jay Pritchett', 'Mitchell Pritchett', 'Phil Dunphy'], 
    'Manny Delgado': ['Gloria Pritchett', 'Jay Pritchett', 'Luke Dunphy'], 
    'Mitchell Pritchett': ['Cameron Tucker', 'Claire Dunphy', 'Luke Dunphy'], 
    'Alex Dunphy': ['Luke Dunphy'],
    'Cameron Tucker': ['Gloria Pritchett', 'Mitchell Pritchett'], 
    'Haley Gwendolyn Dunphy': ['Dylan D-Money', 'Gilbert D-Cat'],
    'Phil Dunphy': ['Claire Dunphy', 'Luke Dunphy'],
    'Dylan D-Money': ['Chairman D-Cat', 'Haley Gwendolyn Dunphy'], 
    'Gloria Pritchett': ['Cameron Tucker', 'Jay Pritchett', 'Manny Delgado'], 
    'Luke Dunphy': ['Alex Dunphy', 'Manny Delgado', 'Mitchell Pritchett', 'Phil Dunphy']
}

family_dictionary = []
friends_dictionary = []
last_names = []
for person in person_to_friends:
    family_dictionary.append(person)
    for name in person_to_friends[person]:
        friends_dictionary.append(name)

上面的代码给了我两个列表,我正在尝试使用它们来创建lastnames_to_firstnames字典,其中键是人员的姓氏,值应该是具有此姓氏的人的所有姓名。我不知道怎么从这里开始。

4 个答案:

答案 0 :(得分:2)

您不需要创建两个单独的列表来解决您的问题。您可以使用原始文件进行此操作。以下是您需要做的事情:

  1. 从词典中提取所有名称
  2. 删除重复项
  3. 遍历每个名​​称,分为名字和姓氏,并填充字典
  4. from itertools import chain
    
    n = {}    
    for v in set(chain.from_iterable([k ] + v for k, v in person_to_friends.items())):
         f, l = v.rsplit(None, 1)
         n.setdefault(l, []).append(f)
    
    print(n)
    
    {
        "Delgado": [
            "Manny"
        ],
        "D-Cat": [
            "Chairman",
            "Gilbert"
        ],
        "Dunphy": [
            "Claire",
            "Alex",
            "Haley Gwendolyn",
            "Phil",
            "Luke"
        ],
        "D-Money": [
            "Dylan"
        ],
        "Tucker": [
            "Cameron"
        ],
        "Pritchett": [
            "Gloria",
            "Mitchell",
            "Jay"
        ]
    }
    

    此处,person_to_friends是您的输入。您还可以使用collections.defaultdict对象而不是dictsetdefault。我使用itertools.chain来压缩你的字典。使剩下的过程更简单。

    如上所述,这就是您使用defaultdict的方式。

    from collections import defaultdict
    
    n = defaultdict(list)
    for v in set(chain.from_iterable([k ] + v for k, v in person_to_friends.items())):
         f, l = v.rsplit(None, 1)
         n[l].append(f)
    

    这恰好比dict + setdefault更有效率。

答案 1 :(得分:0)

如果您的字符串格式为<FirstName> <LastName><FirstName> <Middle> <LastName>且仅以空格分隔,则应查看split function.

答案 2 :(得分:0)

你可以试试这个;

from collections import defaultdict
import itertools
d = defaultdict(list)
person_to_friends = {'Jay Pritchett': ['Claire Dunphy', 'Gloria Pritchett', 'Manny Delgado'], 
'Claire Dunphy': ['Jay Pritchett', 'Mitchell Pritchett', 'Phil Dunphy'], 
'Manny Delgado': ['Gloria Pritchett', 'Jay Pritchett', 'Luke Dunphy'], 
'Mitchell Pritchett': ['Cameron Tucker', 'Claire Dunphy', 'Luke Dunphy'], 
'Alex Dunphy': ['Luke Dunphy'],
'Cameron Tucker': ['Gloria Pritchett', 'Mitchell Pritchett'], 
'Haley Gwendolyn Dunphy': ['Dylan D-Money', 'Gilbert D-Cat'],
'Phil Dunphy': ['Claire Dunphy', 'Luke Dunphy'],
'Dylan D-Money': ['Chairman D-Cat', 'Haley Gwendolyn Dunphy'], 
'Gloria Pritchett': ['Cameron Tucker', 'Jay Pritchett', 'Manny Delgado'], 
'Luke Dunphy': ['Alex Dunphy', 'Manny Delgado', 'Mitchell Pritchett', 'Phil Dunphy']
}
new_people = list(itertools.chain.from_iterable([[i.split() for i in [a]+b] for a, b in person_to_friends.items()]))
for i in new_people:
   d[i[-1]].append(i[0])

final_list = {a:list(set(b)) for a, b in d.items()}

输出:

{'Dunphy': ['Alex', 'Luke', 'Claire', 'Phil', 'Haley'], 'D-Money': ['Dylan'], 'Tucker': ['Cameron'], 'D-Cat': ['Gilbert', 'Chairman'], 'Delgado': ['Manny'], 'Pritchett': ['Mitchell', 'Gloria', 'Jay']}

不导入任何内容:

d = {}
new_people = [[i.split() for i in [a]+b] for a, b in person_to_friends.items()]
for listing in new_people:
    for i in listing:
        if i[-1] in d:
           d[i[-1]].append(i[0])
        else:
           d[i[-1]] = [i[0]]

d = {a:list(set(b)) for a, b in d.items()}

输出:

{'Dunphy': ['Alex', 'Luke', 'Claire', 'Phil', 'Haley'], 'D-Money': ['Dylan'], 'Tucker': ['Cameron'], 'D-Cat': ['Gilbert', 'Chairman'], 'Delgado': ['Manny'], 'Pritchett': ['Mitchell', 'Gloria', 'Jay']}

答案 3 :(得分:0)

正如你所说,你有两个要求:

  

我无法导入任何东西,所以有办法吗?

     

键是人的姓氏,值应该是全部   这个姓氏的人的名字。

这里没有任何导入方法:

person_to_friends = {
    'Jay Pritchett': ['Claire Dunphy', 'Gloria Pritchett', 'Manny Delgado'],
    'Claire Dunphy': ['Jay Pritchett', 'Mitchell Pritchett', 'Phil Dunphy'],
    'Manny Delgado': ['Gloria Pritchett', 'Jay Pritchett', 'Luke Dunphy'],
    'Mitchell Pritchett': ['Cameron Tucker', 'Claire Dunphy', 'Luke Dunphy'],
    'Alex Dunphy': ['Luke Dunphy'],
    'Cameron Tucker': ['Gloria Pritchett', 'Mitchell Pritchett'],
    'Haley Gwendolyn Dunphy': ['Dylan D-Money', 'Gilbert D-Cat'],
    'Phil Dunphy': ['Claire Dunphy', 'Luke Dunphy'],
    'Dylan D-Money': ['Chairman D-Cat', 'Haley Gwendolyn Dunphy'],
    'Gloria Pritchett': ['Cameron Tucker', 'Jay Pritchett', 'Manny Delgado'],
    'Luke Dunphy': ['Alex Dunphy', 'Manny Delgado', 'Mitchell Pritchett', 'Phil Dunphy']
}

last_names=["".join(i.split()[-1:]) for i in person_to_friends]
lastnames_to_firstnames ={}
for key,items in person_to_friends.items():
    for last_name in last_names:
        for names in items:
            if last_name in names:
                if last_name not in lastnames_to_firstnames :
                    lastnames_to_firstnames[last_name]=[names]
                else:
                    if names not in lastnames_to_firstnames .get(last_name):
                        lastnames_to_firstnames[last_name].append(names)

print(lastnames_to_firstnames)

输出:

{'D-Money': ['Dylan D-Money'], 'Tucker': ['Cameron Tucker'], 'Pritchett': ['Gloria Pritchett', 'Mitchell Pritchett', 'Jay Pritchett'], 'Dunphy': ['Haley Gwendolyn Dunphy', 'Alex Dunphy', 'Phil Dunphy', 'Claire Dunphy', 'Luke Dunphy'], 'Delgado': ['Manny Delgado']}