我有一张图例列表 看起来像这样的东西:
[( abc , 121),( abc , 231),( abc , 148), ( abc ,221)]
我想用图普尔斯内部的整数值来按升序排序这个列表。 有可能吗?
尝试使用 key
key 关键词参数 sorded ()
, 以默认的递增顺序排序 :
sorted(
[( abc , 121), ( abc , 231), ( abc , 148), ( abc , 221)],
key=lambda x: x[1]
)
key
应该是一个函数,用以识别如何从数据结构中获取可比元素。在您的情况下,它是图普的第二个元素,因此我们访问 [1]
。
关于优化,请参见 jamilak S 使用 < code> operator. ititgetter(1) 的响应,该响应基本上是 lambda x: x[1]
的更快版本。
>>> from operator import itemgetter
>>> data = [( abc , 121),( abc , 231),( abc , 148), ( abc ,221)]
>>> sorted(data,key=itemgetter(1))
[( abc , 121), ( abc , 148), ( abc , 221), ( abc , 231)]
IMO using itemgetter
is more readable in this case than the solution by @cheeken. It is
also faster since almost all of the computation will be done on the c
side (no pun intended) rather than through the use of lambda
.
>python -m timeit -s "from operator import itemgetter; data = [( abc , 121),( abc , 231),( abc , 148), ( abc ,221)]" "sorted(data,key=itemgetter(1))"
1000000 loops, best of 3: 1.22 usec per loop
>python -m timeit -s "data = [( abc , 121),( abc , 231),( abc , 148), ( abc ,221)]" "sorted(data,key=lambda x: x[1])"
1000000 loops, best of 3: 1.4 usec per loop
Adding to Cheeken s answer, This is how you sort a list of tuples by the 2nd item in descending order.
sorted([( abc , 121),( abc , 231),( abc , 148), ( abc ,221)],key=lambda x: x[1], reverse=True)
作为匹顿新植物,我只想提一下 如果数据真的看起来是这样的:
data = [( abc , 121),( abc , 231),( abc , 148), ( abc ,221)]
然后 sorded ()
将自动按图普的第二个元素排序, 因为第一个元素完全相同 。
用于某种就地类型,使用
foo = [(list of tuples)]
foo.sort(key=lambda x:x[0]) #To sort by first element of the tuple
Python 维基语:
>>> from operator import itemgetter, attrgetter
>>> sorted(student_tuples, key=itemgetter(2))
[( dave , B , 10), ( jane , B , 12), ( john , A , 15)]
>>> sorted(student_objects, key=attrgetter( age ))
[( dave , B , 10), ( jane , B , 12), ( john , A , 15)]
对于羊羔回避方法,首先定义您自己的函数 :
def MyFn(a):
return a[1]
然后:
sorted([( abc , 121),( abc , 231),( abc , 148), ( abc ,221)], key=MyFn)
将 Python 2.7/code> 改为
Python 2.7/code>,这样可以使被接受的回答略为易读:
sorted([( abc , 121),( abc , 231),( abc , 148), ( abc ,221)], key=lambda (k, val): val)
OP 中的排序值是整数这一事实与问题本身无关。 换句话说, 如果排序值是文字, 被接受的答案会有效 。 我提出这一点还想指出, 在排序中可以修改该分类( 例如, 以计算大小例 ) 。
>>> sorted([(121, abc ), (231, def ), (148, ABC ), (221, DEF )], key=lambda x: x[1])
[(148, ABC ), (221, DEF ), (121, abc ), (231, def )]
>>> sorted([(121, abc ), (231, def ), (148, ABC ), (221, DEF )], key=lambda x: str.lower(x[1]))
[(121, abc ), (148, ABC ), (231, def ), (221, DEF )]
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 ]="...