是否有可能在朱莉娅排序字典?

时间:2015-04-24 13:26:13

标签: sorting dictionary data-structures julia

我使用zip()之类的

创建了两个数组中的字典
list1 = [1,2,3,4,5]
list2 = [6,7,8,9,19]
dictionary1 = Dict(zip(list1,list2))

现在我想按key(list1)list2对此词典进行排序。有人可以告诉我一个方法或功能,如何实现它?

2 个答案:

答案 0 :(得分:16)

(希望为@ Simon的答案添加评论,但不足以代表)

排序还会使用.container { position:relative; float:left; } 关键字,这意味着您可以执行

by

另请注意,DataStructures.jl中有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) ,它维护排序顺序,并且SortedDict维护插入顺序。最后,有一个pull请求允许直接排序OrderedDict(但我需要完成它并提交它)。

答案 1 :(得分:5)

虽然如果需要对字典进行排序,SortedDict可能很有用,但通常只需要对字典进行排序以进行输出,在这种情况下,可能需要以下内容:

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

......产生:

5-element Array{(Int64,Int64),1}:
 (1,6) 
 (2,7) 
 (3,8) 
 (4,9) 
 (5,19)

我们可以按以下方式对值进行排序:

sort(collect(zip(values(dictionary1),keys(dictionary1))))

...给出:

5-element Array{(Int64,Int64),1}:
 (6,1) 
 (7,2) 
 (8,3) 
 (9,4) 
 (19,5)