简体   繁体   English

python词典列表排序多个键

[英]python list of dictionaries sort multiple keys

I have a list of dictionaries that have keys of path and type (type 0 => file and type 1 => directory) initial looks like this 我有一个字典列表,其中包含pathtype键(类型0 =>文件和类型1 =>目录)初始看起来像这样

everything = [{u'path': u'/1/', u'type': 1},
     {u'path': u'/2/', u'type': 1},
     {u'path': u'/3/', u'type': 1},
     {u'path': u'/1/something/a.py', u'type': 0},
     {u'path': u'/1/something/b.py', u'type': 0},
     {u'path': u'/1/something/c.py', u'type': 0},
     {u'path': u'/1/foobar/', u'type': 1},
     {u'path': u'/2/baz/', u'type': 1},
     {u'path': u'/1/something/baz/snap/pop/a.py', u'type': 0},
     {u'path': u'/1/something/baz', u'type': 1}]

I want to first sort on path then off of type so I get something like this 我想首先在path排序然后关闭type所以我得到这样的东西

everything = [{u'path': u'/1/', u'type': 1},
     {u'path': u'/1/foobar/', u'type': 1},
     {u'path': u'/1/something/baz', u'type': 1}, # < --- I want this here
     {u'path': u'/1/something/a.py', u'type': 0},
     {u'path': u'/1/something/b.py', u'type': 0},
     {u'path': u'/1/something/baz/snap/pop/a.py', u'type': 0},
     {u'path': u'/1/something/c.py', u'type': 0},
     {u'path': u'/2/', u'type': 1},
     {u'path': u'/2/baz/', u'type': 1},
     {u'path': u'/3/', u'type': 1}]

where the "something" section starts with type 1. 其中“something”部分以类型1开头。

I thought when I do 当我这么想时我想

everything.sort(key=lambda x: (x['path'], x['type']))

I would get what I want, but I get 我会得到我想要的,但我明白了

everything = [{u'path': u'/1/', u'type': 1},
     {u'path': u'/1/foobar/', u'type': 1},
     {u'path': u'/1/something/a.py', u'type': 0},
     {u'path': u'/1/something/b.py', u'type': 0},
     {u'path': u'/1/something/baz', u'type': 1}, # < --- I don't want this here
     {u'path': u'/1/something/baz/snap/pop/a.py', u'type': 0},
     {u'path': u'/1/something/c.py', u'type': 0},
     {u'path': u'/2/', u'type': 1},
     {u'path': u'/2/baz/', u'type': 1},
     {u'path': u'/3/', u'type': 1}]

Is there an easy way to sort everything in this fashion or do I have to write my own sort? 是否有一种简单的方法可以按照这种方式对everything进行排序,还是我必须编写自己的排序?

EDIT: 编辑:

Perhaps this will help explain what I want. 也许这有助于解释我想要的东西。

In linux when you do a ls -lR you get a nice list of dirs and files in the dirs listed under it. 在Linux中,当你执行ls -lR你会在它下面的目录中得到一个很好的目录和文件列表。

Thanks 谢谢

You need to only include in the key what you actually want to be sorted. 您只需要在密钥中包含您实际要排序的内容。 Since you appear to want to reduce the priority of everything after the last slash-delimited segment in the path, you should remove that or move it later in the sort order: 由于您似乎希望在路径中最后一个斜杠分隔的段之后减少所有内容的优先级,因此您应该删除它或稍后按排序顺序移动它:

everything.sort(key=lambda x: (
    '/'.join(x['path'].split('/')[:-1]),
    x['type'],
    ''.join(x['path'].split('/')[-1:]),
  )
)

你的排序标准是如此深奥,我认为你只能通过编写比较函数来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM