[英]How to efficiently order a list of lists using a list in python
I have a list of lists as follows.我有一个列表如下。
mylist = [['dog', ['dogs details']], ['cat', ['cats details']], ['rat', ['rats details']], ['rabbit', ['rabbits details']], ['mouse', ['mice details']], ['goat', ['goats details']]]
I also have a names list as follows.我也有一个名单如下。
mynameslist = ["fish", "rabbit", "cow", "cat", "dog", "owl", "rat", "mouse", "parrot", "goat", "emjenne", "emj"]
I want to order mylist
considering the order of mynameslist
.考虑到
mynameslist
的顺序,我想订购mylist
。 ie my output should be as follows.即我的output应该如下。
mylist_new = [['rabbit', ['rabbits details']], ['cat', ['cats details']], ['dog', ['dogs details']], ['rat', ['rats details']], ['mouse', ['mice details']], ['goat', ['goats details']]]
I tried to do this as follows.我试图这样做如下。
mylist_new = []
for item in mynameslist:
for val in mylist:
if val[0] == item:
mylist_new.append(val)
break
However, I have really long mylist
and mynameslist
.但是,我的
mylist
和mynameslist
真的很长。 Since my code is O(n^2) it takes a lot of time to create mylist_new
.由于我的代码是 O(n^2) ,因此创建
mylist_new
需要花费大量时间。 I am wondering if there is a more pythonic way of doing this with less time.我想知道是否有一种更 Pythonic 的方式可以用更少的时间做到这一点。
I am happy to provide more details if needed.如果需要,我很乐意提供更多详细信息。
Create a map from animal name to index:从动物名称到索引创建一个map :
>>> namemap = {e:i for i,e in enumerate(mynameslist)}
And then use that as a key for sorted
:然后将其用作
sorted
的键:
>>> sorted(mylist, key=lambda x: namemap[x[0]])
[['rabbit', ['rabbits details']], ['cat', ['cats details']], ['dog', ['dogs details']], ['rat', ['rats details']], ['mouse', ['mice details']], ['goat', ['goats details']]]
>>>
This will be O(N*logN) same as sorted
now, since namemap[x[0]]
will be constant time.这将与现在
sorted
的 O(N*logN) 相同,因为namemap[x[0]]
将是恒定时间。
Alternatively, depending on if your data is well behaved, you can do this in O(N), make a map out of your original list:或者,根据您的数据是否表现良好,您可以在 O(N) 中执行此操作,从原始列表中创建一个 map:
>>> animal_map = dict(mylist)
Then iterate over the names list using the map to reconstruct:然后使用 map 遍历名称列表以重建:
>>> [[name, animal_map[name]] for name in mynameslist if name in animal_map]
[['rabbit', ['rabbits details']], ['cat', ['cats details']], ['dog', ['dogs details']], ['rat', ['rats details']], ['mouse', ['mice details']], ['goat', ['goats details']]]
>>>
This will not work if there are duplicates in mylist
如果
mylist
中有重复项,这将不起作用
You can use sorted
or sort
(to sort inplace) with key
parameter like this:您可以使用
sorted
或sort
(就地排序)和key
参数,如下所示:
mylist = [['dog', ['dogs details']], ['cat', ['cats details']], ['rat', ['rats details']], ['rabbit', ['rabbits details']], ['mouse', ['mice details']], ['goat', ['goats details']]]
mynameslist = ["fish", "rabbit", "cow", "cat", "dog", "owl", "rat", "mouse", "parrot", "goat", "emjenne", "emj"]
sorted(mylist, key=lambda x: mynameslist.index(x[0]))
#mylist.sort(key=lambda x: mynameslist.index(x[0]))
Output: Output:
[['rabbit', ['rabbits details']],
['cat', ['cats details']],
['dog', ['dogs details']],
['rat', ['rats details']],
['mouse', ['mice details']],
['goat', ['goats details']]]
Edit: as @juanpa.arrivillaga said, this is fast to implement to slow on complexity (O(N^2)) so if the list is big you should consider using his answer instead编辑:正如@juanpa.arrivillaga 所说,这可以快速实现以降低复杂性(O(N ^ 2))所以如果列表很大,你应该考虑使用他的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.