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

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

问题描述

我已经使用zip()这样从两个数组中创建了一个字典

I have created a dictionary out of two arrays using zip() like

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?

推荐答案

(希望在@Simon的答案中添加评论,但代表人数不足)

(wanted to add a comment to @Simon's answer, but not enough rep)

排序也采用了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,它保持插入顺序.最后,有一个pull请求,它允许直接对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天全站免登陆