排序Python中的对象列表[重复]

时间:2022-05-18 23:39:53

This question already has an answer here:

这个问题在这里已有答案:

In Python, I am parsing an xml file into dictionary, using this module. The core of my code is very simple:

在Python中,我使用此模块将xml文件解析为字典。我的代码的核心很简单:

configdict = ConvertXmlToDict('tasks.xml')

for task in configdict['osmo_tasks']['tasks_entries']['entry']:
    print task['id'], task['due_date'], task['summary']

the above code will parse the xml file into dictionary and then iterate through tasks and print them. Obviously, it will print them in the same order that they were read from, the xml file:

上面的代码将xml文件解析为字典,然后遍历任务并打印它们。显然,它将按照从它们读取的相同顺序打印它们,即xml文件:

1 736366 summary
2 735444 another summary
5 735796 blah

How can I print the lines sorted according to task['due_date'] ?

如何打印根据任务['due_date']排序的行?

This is a sample xml file:

这是一个示例xml文件:

<?xml version="1.0" encoding="utf-8"?>
<osmo_tasks version="000212">
  <category_entries/>
  <tasks_entries>

    <entry>
      <id>1</id>
      <status>1</status>
      <due_date>736366</due_date>
      <due_time>53100</due_time>
      <summary>summary</summary>
    </entry>

    <entry>
      <id>2</id>
      <status>1</status>
      <due_date>735444</due_date>
      <due_time>55800</due_time>
      <summary>another summary</summary>
    </entry>

    <entry>
      <id>5</id>
      <status>0</status>
      <due_date>735796</due_date>
      <due_time>55800</due_time>
      <summary>blah</summary>
    </entry>

  </tasks_entries>
</osmo_tasks>

2 个解决方案

#1


5  

Try the sorted built-in function: https://docs.python.org/2/library/functions.html#sorted

尝试排序的内置函数:https://docs.python.org/2/library/functions.html#sorted

sorted can take any iterable and a key by which to sort. Here's an example similar to yours:

sorted可以采用任何iterable和一个键进行排序。这是一个类似于你的例子:

sorted_test.py

sorted_test.py

configdict = {
        'tasks': {
            'entries': [
                { 'id': 1, 'date': 736366 },
                { 'id': 2, 'date': 735444 },
                { 'id': 3, 'date': 735796 }
                ]
            }
        }

tasks = sorted([ t for t in configdict['tasks']['entries'] ], key=lambda task: task['date'])

print repr(tasks)

Result:

结果:

[{'date': 735444, 'id': 2}, {'date': 735796, 'id': 3}, {'date': 736366, 'id': 1}]

#2


2  

Use sorted with a custom key function key for sorting

使用自定义键功能键排序进行排序

Reference

参考

Something like:

就像是:

sorted(configdict,key=lambda x: x['due_date'])

#1


5  

Try the sorted built-in function: https://docs.python.org/2/library/functions.html#sorted

尝试排序的内置函数:https://docs.python.org/2/library/functions.html#sorted

sorted can take any iterable and a key by which to sort. Here's an example similar to yours:

sorted可以采用任何iterable和一个键进行排序。这是一个类似于你的例子:

sorted_test.py

sorted_test.py

configdict = {
        'tasks': {
            'entries': [
                { 'id': 1, 'date': 736366 },
                { 'id': 2, 'date': 735444 },
                { 'id': 3, 'date': 735796 }
                ]
            }
        }

tasks = sorted([ t for t in configdict['tasks']['entries'] ], key=lambda task: task['date'])

print repr(tasks)

Result:

结果:

[{'date': 735444, 'id': 2}, {'date': 735796, 'id': 3}, {'date': 736366, 'id': 1}]

#2


2  

Use sorted with a custom key function key for sorting

使用自定义键功能键排序进行排序

Reference

参考

Something like:

就像是:

sorted(configdict,key=lambda x: x['due_date'])