Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中对象的排序列表_Python_Sorting_Python 2.7 - Fatal编程技术网

Python中对象的排序列表

Python中对象的排序列表,python,sorting,python-2.7,Python,Sorting,Python 2.7,在Python中,我使用以下方法将xml文件解析到字典中。我的代码的核心非常简单: configdict = ConvertXmlToDict('tasks.xml') for task in configdict['osmo_tasks']['tasks_entries']['entry']: print task['id'], task['due_date'], task['summary'] 上面的代码将把xml文件解析到字典中,然后迭代任务并打印它们。显然,它将按照读取顺序打

在Python中,我使用以下方法将
xml
文件解析到字典中。我的代码的核心非常简单:

configdict = ConvertXmlToDict('tasks.xml')

for task in configdict['osmo_tasks']['tasks_entries']['entry']:
    print task['id'], task['due_date'], task['summary']
上面的代码将把
xml
文件解析到字典中,然后迭代
任务
并打印它们。显然,它将按照读取顺序打印它们,即
xml
文件:

1 736366 summary
2 735444 another summary
5 735796 blah
<?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>
如何打印根据任务['due_date']排序的行?

这是一个示例
xml
文件:

1 736366 summary
2 735444 another summary
5 735796 blah
<?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>

1.
1.
736366
53100
总结
2.
1.
735444
55800
另一个摘要
5.
0
735796
55800
废话

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

比如:

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

尝试
sorted
内置功能:

sorted
可以使用任何iterable和一个键进行排序。下面是一个与您类似的示例:

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)
结果:

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

自定义
函数优于自定义
cmp
——在本例中类似于
操作符.itemgetter('due_date')
。同意。修改答案。