Python OrderedDict 按日期排序

发布于 2024-11-19 17:09:06 字数 204 浏览 4 评论 0原文

我正在尝试使用 OrderedDict (Raymond Hettingers 版本 for pre2.7 Python),其中我的密钥是日期。但是它没有正确排序,我想它可能是根据 ID 排序的。

有人对如何做到这一点有任何建议吗?

I am trying to use an OrderedDict (Raymond Hettingers version for pre2.7 Python) where my keys are dates. However it does not order them correctly, I imagine it may be ordering based on the ID.

Does anyone have any suggestions of how this could be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉生梦死 2024-11-26 17:09:06
In [1]: from collections import OrderedDict

In [2]: import operator

In [3]: from datetime import date

In [4]: d = {date(2012, 1, 1): 123, date(2010,2,5): 542, date(2011,3,3):76 }

In [5]: d # Good old dict
Out[5]: #it seems sorted, but it isn't guaranteed to be that way.
{datetime.date(2010, 2, 5): 542,
 datetime.date(2011, 3, 3): 76,
 datetime.date(2012, 1, 1): 123}

In [6]: o = OrderedDict(sorted(d.items(), key=operator.itemgetter(0)))

In [7]: o #Now it is ordered(and sorted, because we give it by sorted order.).
Out[7]: OrderedDict([(datetime.date(2010, 2, 5), 542), (datetime.date(2011, 3, 3), 76), (datetime.date(2012, 1, 1), 123)])
In [1]: from collections import OrderedDict

In [2]: import operator

In [3]: from datetime import date

In [4]: d = {date(2012, 1, 1): 123, date(2010,2,5): 542, date(2011,3,3):76 }

In [5]: d # Good old dict
Out[5]: #it seems sorted, but it isn't guaranteed to be that way.
{datetime.date(2010, 2, 5): 542,
 datetime.date(2011, 3, 3): 76,
 datetime.date(2012, 1, 1): 123}

In [6]: o = OrderedDict(sorted(d.items(), key=operator.itemgetter(0)))

In [7]: o #Now it is ordered(and sorted, because we give it by sorted order.).
Out[7]: OrderedDict([(datetime.date(2010, 2, 5), 542), (datetime.date(2011, 3, 3), 76), (datetime.date(2012, 1, 1), 123)])
逆光下的微笑 2024-11-26 17:09:06

OrderedDict,根据其文档字符串,是一种记住插入顺序的字典。
因此,您需要以正确的顺序手动插入键/值对。

# assuming unordered_dict is a dict that contains your data 
ordered_dict = OrderedDict()
for key, value in sorted(unordered_dict.iteritems(), key=lambda t: t[0]):
    ordered_dict[key] = value

编辑:请参阅 utdemir 的答案以获得更好的示例。使用 operator.itemgetter 可以为您提供更好的性能(速度提高 60%,我使用下面的基准代码),并且它是更好的编码风格。您可以将 OrderedDict 直接应用于 sorted(...)

a = (1, 2)

empty__func = 0
def empty():
    for i in xrange(N_RUNS):
        empty__func

lambda_func = lambda t: t[0]
def using_lambda():
    for i in xrange(N_RUNS):
        lambda_func(a)

getter_func = itemgetter(0)
def using_getter():
    for i in xrange(N_RUNS):
        getter_func(a)

OrderedDict, according to its docstring, is a kind of dict that remembers insertion order.
Thus, you need to manually insert the key/value pairs in the correct order.

# assuming unordered_dict is a dict that contains your data 
ordered_dict = OrderedDict()
for key, value in sorted(unordered_dict.iteritems(), key=lambda t: t[0]):
    ordered_dict[key] = value

edit: See utdemir's answer for a better example. Using operator.itemgetter gives you better performance (60% faster, I use the benchmark code below) and it's a better coding style. And you can apply OrderedDict directly to sorted(...).

a = (1, 2)

empty__func = 0
def empty():
    for i in xrange(N_RUNS):
        empty__func

lambda_func = lambda t: t[0]
def using_lambda():
    for i in xrange(N_RUNS):
        lambda_func(a)

getter_func = itemgetter(0)
def using_getter():
    for i in xrange(N_RUNS):
        getter_func(a)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文