是否可以在 Julia 中对字典进行排序? [英] Is it possible to sort a dictionary in Julia?

查看:27
本文介绍了是否可以在 Julia 中对字典进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 zip() 之类的

list1 = [1,2,3,4,5]
list2 = [6,7,8,9,19]
dictionary1 = Dict(zip(list1,list2))

现在我想按 key(list1)list2 对字典进行排序.有人可以告诉我一种方式或功能,如何实现吗?

Now i want to sort this dictionary by key(list1) or by list2. Can somebody show me a way or function, how to realize it?

推荐答案

Sort 也需要一个 by 关键字,这意味着你可以这样做:

Sort also takes a by keyword, which means you can do:

julia> sort(collect(dictionary1), by=x->x[2])
5-element Array{Tuple{Int64,Int64},1}:
 (1,6)
 (2,7)
 (3,8)
 (4,9)
 (5,19)

还要注意,DataStructures.jl 中有一个 SortedDict 维护排序顺序,还有一个 OrderedDict 维护insertion 顺序.最后,有一个拉取请求,它允许直接对 OrderedDicts 进行排序(但我需要完成并提交它).

Also note that there is a SortedDict in DataStructures.jl, which maintains sort order, and there's an OrderedDict which maintains insertion order. Finally, there's a pull request which would allow direct sorting of OrderedDicts (but I need to finish it up and commit it).

这篇关于是否可以在 Julia 中对字典进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆