python3 floating point sorting

Python2 has many floating-point number sorting methods, but it is very inconvenient that there is no cmp parameter in the sort function of Python3. Here are several floating-point number sorting methods suitable for Python3
Original text AddressPython | Ways to sort list of float values

Method 1: lambda expression

# 浮点数排序

# 列表初始化
Input = [12.8, .178, 1.8, 782.7, 99.8, 8.7] 

# 用lambda进行排序
Output = sorted(Input, key = lambda x:float(x)) 

# 打印输出
print(Output)

Method 2: Use the sorting method sorted

# 列表初始化
Input = [12.8, .178, 1.8, 782.7, 99.8, 8.7] 

# 使用sorted+key
Output = sorted(Input, key = float) 

# 打印输出
print(Output) 

Method 3: Use sort

# Python code to sort list of decimal values 

# 列表初始化
Input = [12.8, .178, 1.8, 782.7, 99.8, 8.7] 

# 使用sort+key
Input.sort(key = float) 

# 打印输出
print(Input) 

The above are the common methods of python3 floating point calculation. Thank you to the original author for sharing.

おすすめ

転載: blog.csdn.net/weixin_42474261/article/details/104852271