如何使用具有唯一键的多个列表创建字典

时间:2017-07-02 14:09:54

标签: python python-3.x

我正在尝试从预定义列表中为现有字典添加键值。以下是一个例子:

# I created players dictionary : 
players = {'gyms_visited': [],
 'player_id': [4],
 'player_name': 'cynthia',
 'player_pokemon': {},
 'time_played': 30.9}

我正在尝试将列表ab添加到我在上面创建的播放器词典中:

a = ['player_id', 'player_name','time_played', 'player_pokemon', 'gyms_visited']
b = [2, 'teri', 22.2, {}, []]

另外,有没有办法让Player_id的值(4,2)作为玩家字典的键(这就是我想要达到的目的):

{4: {'gyms_visited': [],
     'player_name': 'cynthia',
     'player_pokemon': {},
     'time_played': 30.9},
 2: {'player_name':'teri',
     'time_played':22.2,
     'gyms_visited': [],
     'player_pokemon':{}}

2 个答案:

答案 0 :(得分:0)

您只需要:

# to change already existing dict
players = {players['player_id']:{key:value for key,value in players.items() if key != 'player_id'}}
# make dict from lists a and b
z = {b[a=='player_id']: {key:value for key,value in zip(a,b) if key !='player_id'}}
# group both
players={**players,**z}

下面的详细信息:

首先,您需要将已经制作的dict更改为所需的输出类型,如下所示:

players = {players['player_id']:{key:value for key,value in players.items() if key != 'player_id'}}
print(players)

将输出:

{4: {'gyms_visited': [], 
    'player_name': 'cynthia', 
    'player_pokemon': {}, 
    'time_played': 30.9}}

然后你拿走你的两个列表ab,然后像这样制作一个额外的词典:

z = {b[a=='player_id']: {key:value for key,value in zip(a,b) if key !='player_id'}}
print(z)

将输出:

{2: {'player_name':
    'teri', 'time_played': 22.2, 
    'player_pokemon': {}, 
    'gyms_visited': []}}

最后,你只需将它们放在同一个字典中,如下所示:

players={**players,**z}
print(players)

输出:

{4: {'gyms_visited': [],
    'player_name': 'cynthia',
    'player_pokemon': {},
    'time_played': 30.9},
 2: {'player_name': 'teri',
    'time_played': 22.2, 
    'player_pokemon': {},
    'gyms_visited': []}}

请注意,如果要添加更多元素,可以循环执行此操作。例如,请考虑以下新列表:

a = ['player_id', 'player_name','time_played', 'player_pokemon', 'gyms_visited']
b = [6, 'new_Name', 66.6, {}, []]

与以前一样:

z = {b[a=='player_id']: {key:value for key,value in zip(a,b) if key !='player_id'}}
players={**players,**z}

你得到输出:

{4: {'gyms_visited': [], 
    'player_name': 'cynthia', 
    'player_pokemon': {}, 
    'time_played': 30.9}, 
 2: {'player_name': 'teri', 
    'time_played': 22.2, 
    'player_pokemon': {}, 
    'gyms_visited': []}, 
 6: {'player_name': 'new_Name', 
     'time_played': 66.6, 
     'player_pokemon': {}, 
     'gyms_visited': []}}

您可以参考有关merging dicts**dicts的这些链接,以便更好地理解。

这里也是内置zip方法的link

答案 1 :(得分:0)

from itertools import izip
from pprint import pprint

# Edit your existing dict
>>> players2 = {players.pop('player_id')[0]: players}
>>> pprint(players2)
{4: {'gyms_visited': [],
     'player_name': 'cynthia',
     'player_pokemon': {},
     'time_played': 30.9}}

# Add new player info
>>> players2.update({b[0]: {k: v for k, v in izip(a, b[1:])}})
>>> pprint(players2)
{2: {'gyms_visited': [],
     'player_name': 'teri',
     'player_pokemon': {},
     'time_played': 22.2},
 4: {'gyms_visited': [],
     'player_name': 'cynthia',
     'player_pokemon': {},
     'time_played': 30.9}}

注意:每当您使用某些迭代代码时,都值得查看内置的itertools package。对于eaxample,izip通过返回迭代器而不是列表来改进zip