Some common methods --argsort numpy narray data structure, sort, and a built-in method sorted

numpy.argsort

This method is a sorted array inputted, returns a new array, but the array elements is the index corresponding to each element's position in the original array

x= np. array([3,1,2])
np.argsort(x)

>>> array([1,2,0])

This function may be found is by default ascending

If it is a multidimensional array, you can modify the axis keywords

x = np.array([ [0,3],[2,2] ])
>>>x
> array([[0,3],
>        [2,2]
> ]
>>>np.argsort(x,axis = 0)
> array([[0,1],
>        [1,0]
> ])

If you want to sort from high to low, so you can use

np.argsort( -x )

sorted

This method is built python method may return from iterables in a sorted list of new
prototype
sorted (iterable [, cmp, [
, key [, reverse = True]]]) Parameters:
CMP : Specifies a custom comparison function that takes two arguments (iterables element), if the first parameter is less than the second argument and return a negative number; if the first parameter is equal to the second argument, returns zero; if the first parameter is greater than the second parameter and returns an integer, the default value None.
Key : Specifies a function that accepts a parameter, a function for extracting a keyword from the comparison for each element. The default value is None (should be the element itself)
Reverse : is a Boolean value. If set to True, the list elements will be the reverse order.

Simple usage

>>>sorted([5,4,3,2,1])
>[1,2,3,4,5]

Method list.sort (), efficiency will be higher, because it is the place to sort, but refers to is useful for the list, not as good as sorted convenient

sorted({1:'D',2:'B',3:'B',4:"E",5:'A'})   [1,2,3,4,5]

This returns a list of a key component, the default is to use as a keyword dictionary to sort the keys

Sometimes you can specify your own keywords, such as

>>>sorted("This is a test string from Andrew".split(), key=str.lower) 
>['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']

Compare keywords is str.lower, regardless of capitalization compare

For complex objects above comparison is to use the slice object as a keyword. for

>>>student_tuples = [ ('john','A',15),( 'jane','B',12 ),('dave','B',10) ]
>>>sorted( student_tuples, key = lambda student: student[2] )
>[('dave','B',10),( 'jane','B',12 ),('john','A',15)]

The above is the use of age as a sort keywords

Using the operator's itemgetter and attrgetter function to sort
using the key keyword custom comparison function method is very common, so the operator module provides itemgetter and methods of attrgetter to compare keyword specified

from operator import itemgetter,attrgetter
sorted( student_tuples,key = itemgetter(2) ) [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

That value iterables elements of the second position, as a comparison of the key value.
If the element is a plurality of attributes, the object can also be used to compare attrgetter specified object name

sorted(student_objects,key = attrgetter('age') 

operator module also supports multi-level sorting, such as press performance, after age

sorted(student_tuples, key=itemgetter(1,2)) [('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)] 
sorted(student_objects, key=attrgetter('grade', 'age')) [('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]

There is such a piece of code on the machine learning combat

sortedClassCount = sorted( classCount.iteritems(),key = itemgetter(1),reverse = True )

classcount is a dictionary, iteritems (middle) will key-value pair is converted to the form of tuples such as ( 'B', 15), forming a list of the original group, then after key = itemgetter (1) with the significance of the latter is voting value in descending order.

3.0 In no iteritems this member function, but there are items function, can be converted into a dictionary can be iterable

 >> mat.items()
 >>dict_items([(1, 2), (3, 4)])

Initial feeling, if itemgetter encounter with attrgetter have multiple properties of objects, you can set up multiple priority sorting complex

Guess you like

Origin blog.csdn.net/ronaldo_hu/article/details/51240745