按引用排序字典?(Python 3.10)

huangapple go评论68阅读模式
英文:

Sort Dictionary by Reference? (Python 3.10)

问题

我试图在Python中按升序键顺序对字典进行排序。我试图在函数内部完成这个操作,将字典作为参数提供。

我了解Python中有可变和不可变的数据类型,我知道字典应该是可变的数据类型。也就是说,我应该能够通过引用传递它作为函数参数。

通常情况下,我会使用dict(sorted(d.items()))方法,但是当通过引用提供字典时,似乎不起作用。

这里有一个示例:

def CreateDict(d):
    d[0] = "a"
    d[4] = "b"
    d[2] = "c"
    d[1] = "d"

def SortDict(d):
    d = dict(sorted(d.items()))

def main():
    d = {}

    print(d)
    CreateDict(d)
    print(d)
    SortDict(d)
    print(d)

if (__name__ == '__main__'):
    main()

CreateDict函数中,我向字典d中添加了一些元素,并尝试在SortDict函数中对其进行排序。但是,我得到的输出是以下内容:

{}
{0: 'a', 4: 'b', 2: 'c', 1: 'd'}
{0: 'a', 4: 'b', 2: 'c', 1: 'd'}

即使在调用SortDict后,d仍然保持未排序状态。

当作为函数参数传递时,正确的方式是什么呢?

感谢阅读我的帖子,任何指导都会受到赞赏。

英文:

I'm attempting to sort a dictionary in Python in ascending key order. I'm trying to do this inside of a function, with the dictionary being supplied as an argument.

I understand that there are mutable and immutable datatypes in Python, and I understand that a dictionary is supposed to be a mutable datatype. That is, I should be able to pass it by-reference when supplying it as a function argument.

Usually, I would use the dict(sorted(d.items())) method, however that does not appear to work when supplying a dictionary by reference.

Here's an example:

def CreateDict(d):
    d[0] = "a"
    d[4] = "b"
    d[2] = "c"
    d[1] = "d"

def SortDict(d):
    d = dict(sorted(d.items()))

def main():
    d = {}

    print(d)
    CreateDict(d)
    print(d)
    SortDict(d)
    print(d)

if (__name__ == '__main__'):
    main()

Where I add some elements into the dictionary d in the CreateDict function, and I try to sort it in the SortDict function. However, the output I get is the following:

{}
{0: 'a', 4: 'b', 2: 'c', 1: 'd'}
{0: 'a', 4: 'b', 2: 'c', 1: 'd'}

With d remaining unsorted even after a call to SortDict.

What is the proper way of sorting a Python dictionary when supplied by-reference as a function argument?

Thanks for reading my post, any guidance is appreciated.

答案1

得分: 3

用引用作为函数参数时,对Python字典进行正确排序的方法是什么?

我会说最好的方法是返回新的字典,让调用者使用返回值。

但是,如果您坚持要修改现有的字典,下面有几种方法:

def SortDict(d):
    items = sorted(d.items())
    d.clear()
    d |= items

def SortDict(d):
    for k in sorted(d):
        d[k] = d.pop(k)

def SortDict(d):
    k = sorted(d)
    d |= zip(k, map(d.pop, k))
英文:

> What is the proper way of sorting a Python dictionary when supplied by-reference as a function argument?

I'd say it's to return the new dict, and have the caller use the return value.

But, if you insist on modifying the existing dict, here are a few ways:

def SortDict(d):
    items = sorted(d.items())
    d.clear()
    d |= items

def SortDict(d):
    for k in sorted(d):
        d[k] = d.pop(k)

def SortDict(d):
    k = sorted(d)
    d |= zip(k, map(d.pop, k))

huangapple
  • 本文由 发表于 2023年6月26日 09:20:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553024.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定