我有四条dict(途径4)清单,其中列举这样的内容:
[{ id : 1 , name : alfa },{ id : 2 , name : bravo },{ id : 3 , name : charlie },{ id : 4 , name : delta }]
我完全知道我想他们的话,那就是:
2, 3, 1, 4
重新排列顺序的最简单方式是什么?
我有四条dict(途径4)清单,其中列举这样的内容:
[{ id : 1 , name : alfa },{ id : 2 , name : bravo },{ id : 3 , name : charlie },{ id : 4 , name : delta }]
我完全知道我想他们的话,那就是:
2, 3, 1, 4
重新排列顺序的最简单方式是什么?
如果它总是四人,而且你总是知道这一命令,那么就好像:
lst = [{...},{...},{...},{...}]
ordered = [lst[1],lst[2],lst[0],lst[3]]
如果你打算id,就应该:
ordered = sorted(lst, key=lambda d: [2,3,1,4].index(int(d[ id ])))
请注意,index(>
>>>
这里具有强行下达通缉令的一般功能(任何重要价值,如不列入通缉令,则在定案清单的末尾处,任意分列):
def ordered(somelist, wantedorder, keyfunction):
orderdict = dict((y, x) for x, y in enumerate(wantedorder))
later = len(orderdict)
def key(item):
return orderdict.get(keyfunction(item), later)
return sorted(somelist, key=key)
页: 1
import operator
sortedlist = ordered(dictlist, ( 2 , 3 , 1 , 4 ),
operator.itemgetter( id ))
非普遍解决办法:
lst = [{ id : 1 , name : alfa },{ id : 2 , name : bravo },{ id : 3 , name : charlie },{ id : 4 , name : delta }]
order = ["2", "3", "1", "4"]
indexes = dict((idfield, index) for (index, idfield) in enumerate(order))
print sorted(lst, key=lambda d: indexes[d["id"]])
# [{ id : 2 , name : bravo }, { id : 3 , name : charlie }, { id : 1 , name : alfa }, { id : 4 , name : delta }]
这里概述:
def my_ordered(it, wanted_order, key):
indexes = dict((value, index) for (index, value) in enumerate(wanted_order))
return sorted(it, key=lambda x: indexes[key(x)])
import operator
print my_ordered(lst, order, operator.itemgetter("id"))
the_list.sort(key=lambda x: (3, 1, 2, 4)[int(x["id"])-1])
更简单的新答案
the_list = [the_list[i - 1] for i in (2, 3, 1, 4)]
这样,欧佩组织就可以看到他所期望的秩序,而这里并不需要这种分类。 这很可能也很快。
如果你想在不考虑法令内容的情况下重新排列顺序:
>>> order = 2, 3, 1, 4
>>> d = [{ id : 1 , name : alfa },{ id : 2 , name : bravo },{ id : 3 , name : charlie },{ id : 4 , name : delta }]
>>> index = dict(enumerate(dd))
>>> [index[i-1] for i in order]
[{ id : 2 , name : bravo }, { id : 3 , name : charlie }, { id : 1 , name : alfa }, { id : 4 , name : delta }]
如果你想把标题按以下顺序排列:
>>> sorted(d, key=lambda x: order.index(int(x[ id ])))
[{ id : 2 , name : bravo }, { id : 3 , name : charlie }, { id : 1 , name : alfa }, { id : 4 , name : delta }]
Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...
I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...
Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...
Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...
I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...
Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...
Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...
I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...