我正在尝试学习 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/