Change language

Python | Sort the list of percentages

Let’s look at different ways to accomplish a task.

Code # 1: Breaks & # 39;% & # 39; in a string and converts it to a floating point number.

 ` # Python code to sort the percent list `   ` # Initialization list ` ` Input ` ` = ` ` [` ` ’2.5%’ ` `, ` ` ’6.4%’ ` `, ` ` ’91.6%’ ` `, ` ` ’11.5%’ ` `] `   ` # remove% and convert to float ` ` # then apply the function sorting ` ` Input ` `. sort (key ` ` = ` ` lambda ` ` x: ` ` float ` ` (x [: ` ` - ` ` 1 ` `])) `   ` # printing ` ` print ` ` (` ` Input ` `) `

Exit:

` [’2.5%’, ’6.4%’, ’11.5%’, ’91.6%’] `

Code # 2:

` `

` # Python code to sort the percent list   # Initialization list Input = [ ’2.5%’ , ’6.4%’ , ’ 91.6% ’ , ’ 11.5% ’ ]   # Temporary initialization list temp = []   # remove the% sign for key in Input : temp.append ((key [: - 1 ]))   # c floating point list formatting temp = sorted (temp, key = float )   # Initialize the output list output = []   # Adding percent sign for key in temp: output.append (key + ’%’ )   # print output print (output) `

` ` Exit:

` [’2.5%’, ’6.4%’, ’11.5%’, ’91.6%’] `

Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method