排序字典键? [英] sorting dictionary keys?

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

问题描述

用键来排序字典

对象的最快方式(打字量最少)是什么?在性能方面,对字典进行迭代的最快方法是什么?

?这两个是等价的吗?


我这样做的方式是:


d = {}

#填充d

keys = d.keys()

keys.sort()
键盘中的k


打印k,d [k]


但是我想这样做:

#error here

for k in d .keys()。sort():

打印k,d [k]


为什么嵌套函数调用不起作用?在此先感谢。

Hi, what''s the fastest way (least amount of typing) to sort dictionary
objects by the key? What''s the fastest way of iterating over sorted keys
for a dictionary in terms of performance? Are the two equivalent?

The way I have been doing it is:

d = {}
#populate d
keys = d.keys()
keys.sort()
for k in keys:
print k, d[k]

but I would like to do:
#error here
for k in d.keys().sort():
print k, d[k]

why doesn''t the nested function call work? Thanks in advance.

推荐答案

John Smith写道:
John Smith wrote:
最快的方式是什么? (打字量最少)


从来没有好的标准

但是我想这样做:
#error here
for k in d.keys()。sort():
打印k,d [k]

为什么嵌套函数调用不起作用?在此先感谢。
Hi, what''s the fastest way (least amount of typing)
Never good criteria
but I would like to do:
#error here
for k in d.keys().sort():
print k, d[k]

why doesn''t the nested function call work? Thanks in advance.




sort()就地排序并返回无



sort() sorts in place and returns None


John Smith
John Smith
通过密钥对
字典对象进行排序的最快方式(最少打字量)是什么?在性能方面,对字典的有序
键进行迭代的最快方法是什么?两者是否相同?

我一直这样做的方式是:

d = {}
#populate d
keys = d.keys ()
keys.sort()
用于键盘中的k:
打印k,d [k]

但我想这样做:
#error在这里
对于d.keys()中的k。sort():


排序到位并且不返回任何内容。在2.4中你会得到

的排序方法。

打印k,d [k]

为什么不是嵌套函数打电话?提前致谢。
Hi, what''s the fastest way (least amount of typing) to sort dictionary objects by the key? What''s the fastest way of iterating over sorted keys for a dictionary in terms of performance? Are the two equivalent?

The way I have been doing it is:

d = {}
#populate d
keys = d.keys()
keys.sort()
for k in keys:
print k, d[k]

but I would like to do:
#error here
for k in d.keys().sort():
sort sorts in place and doesn''t return anything. In 2.4 you''ll have
the sorted method.
print k, d[k]

why doesn''t the nested function call work? Thanks in advance.




输入最短的内容?你在那里用s = d.keys()和

s.sort()。


-


Emile van Sebille
emile @ fenx..com



The shortest thing to type? You''re there with s = d.keys() and
s.sort().

--

Emile van Sebille
emile@fenx..com


Jay O''Connor写道:
Jay O''Connor wrote:
John Smith写道:
John Smith wrote:
>什么是最快的方法(打字量最少)
> Hi, what''s the fastest way (least amount of typing)



从来没有好的标准



Never good criteria

但是我想这样做:
#error这里
对于d.keys()中的k。排序():
打印k,d [k]

为什么不是嵌套函数打电话?在此先感谢。
but I would like to do:
#error here
for k in d.keys().sort():
print k, d[k]

why doesn''t the nested function call work? Thanks in advance.



sort()就地排序并返回无



sort() sorts in place and returns None




< rant>

哪个是Python中的主要PITA之一恕我直言:(

(不是它排序到位的事实,它不会返回自我的事实)。

< / rant>


布鲁诺



<rant>
Which is one of the main PITA in Python IMHO :(
(not the fact that it sort in place, the fact that it does not return self).
</rant>

Bruno


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

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