Technology Programming

Python Sort Function

    The List.sort() Method

    • For sorting lists in place, a programmer would typically use the "sort()" method internal to the List data type. The sort method takes the current list and sorts it to ascending order, as in this example:

      >>>l = [5, 4, 7, 2, 1]

      >>>l.sort()

      >>>l

      [1, 2, 4, 5, 7]

      The list will now remain sorted wherever the programmer should decide to pass it or use it.

    The "sorted()" Method

    • The "sorted()" method performs the same operation on its most basic use. Taking a list, the sorted method will sort the values in the list in ascending order:

      >>>l2 - [6, 8, 4, 3, 2, 5]

      >>>sorted(l2)

      [2, 3, 4, 5, 6, 8]

      Both the list.sort() and sorted() methods also accept a "reverse" argument, which will sort the values in descending order:

      >>>l.sort(reverse=True)

      >>>l

      [7, 5, 4, 2, 1]

      >>>sorted(l2, reverse=True)

      >>>l2

      [8, 6, 5, 4, 3, 2]

    Sorting Lists by Values

    • Certain lists might contain values based on actual organizing data, rather than just a list of numbers or strings. A group of lists that contains an employee's name, age and ID might look similar to these examples:

      >>>e1 = ['Bob', 29, 1]

      >>>e2 = ['Jane', 27, 2]

      >>>e3 = ['Jill', 31, 3]

      A programmer sorting these lists might want to sort by the second value representing age. The programmer can give an extra "key" parameter exclusive to the sorted class which allows the programmer to sort based on specific values, as in this example:

      >>>import operator

      >>>employees = [e1, e2, e3] //a list of employee lists

      >>>sorted(employees, key=operator.itemgetter(1))

      [['Jane', 27, 2], ['Bob', 29, 1], ['Jill', 31, 3]]

    Methods and Searching

    • The sorted class also allows a programmer to sort lists based on the return values of method calls, using the "methodcaller" key as an argument. For example, the programmer might want to sort values based on the number of occurrences of the letter "x" in each entry, as in this example:

      >>>import operator

      >>>l4 = ['rrrxxxyxx', 'xxxtx', 'x']

      >>>sorted(l4, key=operator.methodcaller('count', 'x'))

      ['x', 'xxxtx', 'rrrxxxyxx']

SHARE
RELATED POSTS on "Technology"
WordPress - How to Set up a New Theme to WordPress 3.
WordPress - How to Set up a New Theme to WordPress 3.
Solution of Creative Web Design
Solution of Creative Web Design
The three disciplines of User Experience
The three disciplines of User Experience
Web Design Sheffield Options For Professional Enterprises
Web Design Sheffield Options For Professional Enterprises
Do you have what it takes?
Do you have what it takes?
Segway Cost
Segway Cost
Microsoft Access 2010: What's Coming with Office 2010?
Microsoft Access 2010: What's Coming with Office 2010?
Companies of Web Development in Ireland Provide Designs that Work
Companies of Web Development in Ireland Provide Designs that Work
Penguin Update to Put Red-Flags on Negative SEO
Penguin Update to Put Red-Flags on Negative SEO
Innovative web 2 design templates can make your business famous quickly
Innovative web 2 design templates can make your business famous quickly
Building A Search Engine Friendly Website
Building A Search Engine Friendly Website
Exceptional Advice To Build Up Your Internet Marketing
Exceptional Advice To Build Up Your Internet Marketing
The Benefits of Selecting The Right Hosting Company
The Benefits of Selecting The Right Hosting Company
Is There a Methodology for Making Successful Logos
Is There a Methodology for Making Successful Logos
Benefits of Ruby On Rails Development
Benefits of Ruby On Rails Development
The Power of Colour and Shapes in Your Infant's Life.
The Power of Colour and Shapes in Your Infant's Life.
Advantages of Hiring PSD To HTML Service Providers
Advantages of Hiring PSD To HTML Service Providers
How to Make Responsive Web Design Attractive?
How to Make Responsive Web Design Attractive?
Converting PSD to Responsive HTML
Converting PSD to Responsive HTML
Just a few realy really hints and tips when it comes to website design but look for.
Just a few realy really hints and tips when it comes to website design but look for.

Leave Your Reply

*