按值对 Tcl 字典排序
我想知道 Tcl 中是否有一种按值对 dict 进行排序的优雅方法。
假设我有一个以下字典:
set d1 [dict create k1 10 k2 89 k3 1 k4 15 k5 20]
# Results in dict of form
# k1 => 10
# k2 => 89
# k3 => 1
# k4 => 15
# k5 => 20
现在我想对这个字典进行排序,以便我有:
# k3 => 1
# k1 => 10
# k4 => 15
# k5 => 20
# k2 => 89
我希望有类似于Python的sorted()的东西。
I was wondering if there is an elegant way of sorting dict by value in Tcl.
Suppose I have a following dict:
set d1 [dict create k1 10 k2 89 k3 1 k4 15 k5 20]
# Results in dict of form
# k1 => 10
# k2 => 89
# k3 => 1
# k4 => 15
# k5 => 20
Now I want to sort this dictionary so that I have:
# k3 => 1
# k1 => 10
# k4 => 15
# k5 => 20
# k2 => 89
I was hoping there is something similar to Python's sorted().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有 Tcl 8.6(这利用了字典可以廉价地与列表进行转换的事实):
如果您仍然使用 8.5(可能;8.6 仍处于测试阶段),那么您需要使用几个步骤
:如果您有的话,
-stride
选项会更容易使用。There is, if you have Tcl 8.6 (this uses the fact that dictionaries can be converted cheaply to and from lists):
If you're still on 8.5 (likely; 8.6 is still in beta) then you need to use several steps:
The
-stride
option is easier to use, if you've got it.