我是一名新程序员。我的代码需要帮助。在代码中,我有一个字典,它的值是一个列表。我想在“while”中做一些事情,直到字典有一个值。我使用了 while(len(migration))!= 0
,但它不正确。如何迭代字典中的所有值?
最后,我想删除字典中的值,但我不知道如何执行此操作并更新字典的值。
migration_p = {2: [3, 4], 3: [3]}
def q_sequence():
global p, sequence_q, r_s, d_s, d, partition, migration_p
while len(migration_p) != 0:
sequence_qi = []
migration_pi = copy.deepcopy(migration_p)
while len(migration_pi) != 0:
tqi_min = math.inf
for p in migration_pi.keys():
# if len(migration_pi[p]) < M - A:
for d in migration_pi[p]:
for r_src in migration_pi.keys():
if r_src not in sequence_qi:
xx = time_qi(d, p)[0]
if xx < tqi_min:
tqi_min = xx
r_s = r_src
d_s = d
sequence_qi.append([(time_qi(d, p)[1], d_s), r_s])
migration_pi = {p: d for p, d in migration_pi.items() if p != r_s}
migration_pi = {p: d for p, d in migration_pi.items() if (p != r_s or d != d_s)}
sequence_q.append(sequence_qi)
# print(sequence_q)
break
return sequence_q
我希望输出为 [[[(1, 3), 2], [(1, 3), 3]]]
但实际输出为 [[[ (1, 3), 2], [(1, 3), 3], [(2, 4), 2]]]
.
最佳答案
迭代值
for value in dictionary.values():
...
关于python - 如何迭代字典中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56455158/