python解释器在调用导入函数后返回空白字典

标签 python function dictionary import interpreter

我正在尝试学习 python(具有 VBA 背景)。

我已将以下函数导入到我的解释器中:

def shuffle(dict_in_question):  #takes a dictionary as an argument and shuffles it
    shuff_dict = {}
    n = len(dict_in_question.keys())
    for i in range(0, n):
        shuff_dict[i] = pick_item(dict_in_question)
    return shuff_dict

以下是我的翻译的打印内容;

>>> stuff = {"a":"Dave", "b":"Ben", "c":"Harry"}
>>> stuff
{'a': 'Dave', 'c': 'Harry', 'b': 'Ben'}
>>> decky11.shuffle(stuff)
{0: 'Harry', 1: 'Dave', 2: 'Ben'}
>>> stuff
{}
>>> 

看起来字典被打乱了,但之后字典是空的。为什么?还是我用错了?

最佳答案

您还需要将其分配回 stuff,因为您要返回一本新字典。

>>> stuff = decky11.shuffle(stuff)

关于python解释器在调用导入函数后返回空白字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11806084/

相关文章:

python - 将随机数生成器写入文本文件

javascript - 无法弄清楚为什么参数在 JS fibonacci 函数的递归过程中发生变化

list - 在 common Lisp 中将函数列表作为参数传递

python - 如何在一个表达式中合并两个字典?

java - 如何在不使用 Collections.sort() 的情况下按值对 LinkedHashMap 进行排序?

python - 使用 round() 对连续值进行分箱会创建工件

python celery - 如何在运行时向工作人员添加 CELERYBEAT_SCHEDULE 任务?

python - 如何对 url 中发布的字典的键进行排序?

function - 这个Go函数类型 "HandlerFunc"是怎么工作的,来自标准库 "net/http"

Python 按第一个键对二维字典进行排序