Python|对百分比列表进行排序
给定一个百分比列表,编写一个Python程序以升序对给定列表进行排序。
让我们看看完成任务的不同方法。
代码 #1:将字符串中的 '%' 截断并将其转换为浮点数。
# Python code to sort list of percentage
# List initialization
Input =['2.5 %', '6.4 %', '91.6 %', '11.5 %']
# removing % and converting to float
# then apply sort function
Input.sort(key = lambda x: float(x[:-1]))
# printing output
print(Input)
输出:
['2.5 %', '6.4 %', '11.5 %', '91.6 %']
代码#2:
# Python code to sort list of percentage
# List initialization
Input =['2.5 %', '6.4 %', '91.6 %', '11.5 %']
# Temporary list initialization
temp = []
# removing % sign
for key in Input:
temp.append((key[:-1]))
# sorting list of float
temp = sorted(temp, key = float)
# Output list initialization
output = []
# Adding percentage sign
for key in temp:
output.append(key + '%')
# printing output
print(output)
输出:
['2.5 %', '6.4 %', '11.5 %', '91.6 %']