python - 如何迭代字典中的值?

标签 python

我是一名新程序员。我的代码需要帮助。在代码中,我有一个字典,它的值是一个列表。我想在“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/

相关文章:

python - python中的文件读取和for循环

python - 通过 tf-idf 特征选择重用 sklearn 文本分类模型

python - 是否可以交替使用 *args 和 **kwargs?

python - Pandas :如何识别具有 dtype 对象但混合类型项目的列?

Python:loadtxt:从文件中读取数字数据和注释行

python - 保持 numpy 中比率的准确性

python - 返回函数的函数(在 python 中)

python - Keras( tensorflow 后端)获取 "TypeError: unhashable type: ' Dimension'"

Python 处理 TypeError :

python - gensim 生成LSI模型导致 "Python has stopped working"