我有一个嵌套的字典:
my_dict = {
1: {'player_id': 1,
'player_name': 'Bryan_Demapan',
'time_played': 0.0,
'player_pokemon': {},
'gyms_visited': []},
2: {'player_id': 2,
'player_name': 'Tom Syneal',
'gyms_visited': [],
'player_pokemon': {},
'time_played': 0.0}}
假设我有一个列表
new_list = ['A', 'B', 'C']
我该如何做一个for循环语句,将新列表插入到所有带有“ gyms_visited”键的空列表中?
新词典应如下所示
my_dict = {
1: {'player_id': 1,
'player_name': 'Bryan_Demapan',
'time_played': 0.0,
'player_pokemon': {},
'gyms_visited': ['A', 'B', 'C']},
2: {'player_id': 2,
'player_name': 'Tom Syneal',
'gyms_visited': ['A', 'B', 'C'],
'player_pokemon': {},
'time_played': 0.0}}
答案 0 :(得分:3)
只需遍历my_dict
的值并将new_list
分配给键'gyms_visited'
In [529]: for k, v in my_dict.items():
...: v['gyms_visited'] = new_list.copy()
...:
In [530]: my_dict
Out[530]:
{1: {'gyms_visited': ['A', 'B', 'C'],
'player_id': 1,
'player_name': 'Bryan_Demapan',
'player_pokemon': {},
'time_played': 0.0},
2: {'gyms_visited': ['A', 'B', 'C'],
'player_id': 2,
'player_name': 'Tom Syneal',
'player_pokemon': {},
'time_played': 0.0}}
如果仅分配new_list
,则一旦更改new_list
中的任何值,就会遇到麻烦
In [529]: for k, v in my_dict.items():
...: v['gyms_visited'] = new_list
...:
In [531]: new_list[1] = 100
In [532]: my_dict
Out[532]:
{1: {'gyms_visited': ['A', 100, 'C'],
'player_id': 1,
'player_name': 'Bryan_Demapan',
'player_pokemon': {},
'time_played': 0.0},
2: {'gyms_visited': ['A', 100, 'C'],
'player_id': 2,
'player_name': 'Tom Syneal',
'player_pokemon': {},
'time_played': 0.0}}
答案 1 :(得分:0)
尝试一下:
print({idx:{k:(new_list if k=='gyms_visited' else v) for k,v in i.items()} for idx,i in enumerate(my_dict.values(),1)})
嵌套词典理解将起作用。
输出:
{1: {'player_id': 1, 'player_name': 'Bryan_Demapan', 'time_played': 0.0, 'player_pokemon': {}, 'gyms_visited': ['A', 'B', 'C']}, 2: {'player_id': 2, 'player_name': 'Tom Syneal', 'gyms_visited': ['A', 'B', 'C'], 'player_pokemon': {}, 'time_played': 0.0}}
我实际上喜欢@aydow的解决方案,除了它,您还可以像这样:
for k,v in data.items():
v['gyms_visited']=new_list[:]
或者:
import copy
for k,v in data.items():
v['gyms_visited']=copy.deepcopy(new_list)
答案 2 :(得分:0)
您可以使用字典理解:
new_list = ['A', 'B', 'C']
data = {1: {'player_id': 1, 'player_name': 'Bryan_Demapan', 'time_played': 0.0, 'player_pokemon': {}, 'gyms_visited': []}, 2: {'player_id': 2, 'player_name': 'Tom Syneal', 'gyms_visited': [], 'player_pokemon': {}, 'time_played': 0.0}}
new_data = {a:{c:[i for i in new_list if i not in d] if c == 'gyms_visited' else d \
for c, d in b.items()} for a, b in data.items()}
输出:
{
"1": {
"player_id": 1,
"player_name": "Bryan_Demapan",
"time_played": 0.0,
"player_pokemon": {},
"gyms_visited": ["A", "B", "C"]
},
"2": {
"player_id": 2,
"player_name": "Tom Syneal",
"gyms_visited": ["A", "B", "C"],
"player_pokemon": {},
"time_played": 0.0
}
}
答案 3 :(得分:0)
您也可以仅extend()
列表:
for key in my_dict:
my_dict[key]['gyms_visited'].extend(new_list)
并且正如您在此处看到的那样,所有列表都具有不同的id()
并且没有引用相同的对象:
print(id(new_list))
# 2704861952904
for key in my_dict:
print(id(my_dict[key]['gyms_visited']))
# 2704833143368
# 2704833143432
就性能而言,list.extend()
为O(N),其中N = 3是要扩展的列表的长度。这很可能是一系列list.append()
调用,每个调用都是O(1)。
使用[:]
或.copy()
(即O(N))进行复制将是相同的。