Skip to main content

Array sorting in ascending and descending order using python.

 

We need sorted() function to sort the given list in ascending or descending order.

The syntax is as follows: sorted(list, key=..., reverse=....)


>> Lets see first list sorting:- 

#First we need to specify an array and save it in a variable.

a = [2.2, 2.3233, 2322, 32.23, 2, 32.2, 5.11, 4, 0.1, 0.1]


#Secondly we'll be using a sorted() function to sort out the given array.

print('sorting in ascending order:', sorted(a))


#Lastly we'll be using reverse=True for the array to be in descending order.

print('sorting in descending order:', sorted(a, reverse = True))


#Output:

sorting in ascending order: [0.1, 0.1, 2, 2.2, 2.3233, 4, 5.11, 32.2, 32.23, 2322]

sorting in descending order: [2322, 32.23, 32.2, 5.11, 4, 2.3233, 2.2, 2, 0.1, 0.1]


>> Then lets see set{} sorting:-

a = {2.2, 2.3233, 2322, 32.23, 2, 32.2, 5.11, 4, 0.1, 0.1}

#Output:[0.1, 0.1, 2, 2.2, 2.3233, 4, 5.11, 32.2, 32.23, 2322]


>> Tuple() sorting:-

a = (2.2, 2.3233, 2322, 32.23, 2, 32.2, 5.11, 4, 0.1, 0.1)

#Output:[0.1, 0.1, 2, 2.2, 2.3233, 4, 5.11, 32.2, 32.23, 2322]


>> Dictionary{:1, :2} sorting:-

a = {2.2:1, 2.3233:2, 2322:3, 32.23:4, 2:5, 32.2:6, 5.11:7, 4:8, 0.1:9, 0.1:10}

#Output:[0.1, 0.1, 2, 2.2, 2.3233, 4, 5.11, 32.2, 32.23, 2322]


>> Then sorting a string:-

b = 'python is a programming language!'

print(sorted(b))

#Output: [' ', ' ', ' ', ' ', '!', 'a', 'a', 'a', 'a', 'e', 'g', 'g', 'g', 'g', 'h', 'i', 'i', 'l', 'm', 'm', 'n', 'n', 'n', 'o', 'o', 'p', 'p', 'r', 'r', 's', 't', 'u', 'y']

Note: If we sort a string then it will be sorted in alphabetical order, first privilege is given to blank space and special characters. 

Comments

Popular posts from this blog

Glowing Border effect using html/css

  {html code} <html>     <head>         <link href='E:\html\.vscode\.vscode\style.css' type='text/css' rel='stylesheet'>         <title>Glowing Border</title>     </head>     <body>         <div class='box'>             <div class='text'>             <h2><u>Glowing Border</u></h2>             <p>HTML and CSS are technically not the programming languages, they are the scripting languages.              Usually used for the front-end development.</p>             </div>         </div>             </body> </html> {css code} body{     background: black;     display: ...

What are some methods for optimizing Oracle databases for large data inserts?

 For large data inserts I can suggest you few things like: Use trigger(PL/SQL) Use APPEND hint Remove indexes on tables Firstly, while you use triggers in the table it could leave data to be logically corrupt. And it will then perform insert in a very conventional way. Which is a time consuming process and won’t helps us! Secondly, using APPEND hint will help us to an extend. So, APPEND hint tells the optimizer to perform a direct-insert into the table, which improves the performance. Now there is a way which we could achieve this by minimizing the Redo generation. What Redo do is; it basically ensures the recoverability of data to the database. It writes down every transaction to the archive log. Let’s take a scenario, where if the database is running on the NOARCHIVELOG mode, using APPEND hint will reduce the redo generation i.e; it won’t write into the archive log anymore and thus increases the speed. But then it won’t be able to recover at any point in time if your data is ambi...

Quote and Joke Generator

*This a random quote and jokes generator. #The first one is a random quote generator. You will get a new quote every time you click on the button. #The second one is a random joke generator. You will get a new joke every time you click on the button. For Number Guessing Game : click here //click on the button to get a quote //-author get a quote //click on the button to get a joke get a joke