Python如何使用浮点值对列表进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37318637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:13:33  来源:igfitidea点击:

Python how to sort list with float values

pythonlistsorting

提问by sathish

How to sort the python list that contains the float values,

如何对包含浮点值的 python 列表进行排序,

list1 = [1, 1.10, 1.11, 1.1, 1.2] 

or

或者

list1 = ['1', '1.10', '1.11', '1.1', '1.2'] 

The expected results is

预期的结果是

list_val = ['1', **'1.1', '1.2'**, '1.10', '1.11']

but the returned result in using sort() method returns

但是使用 sort() 方法返回的结果返回

[1, 1.1000000000000001, 1.1000000000000001, 1.1100000000000001, 1.2]

or

或者

['1', '1.1', '1.10', '1.11', '1.2'].

But, here 1.2should come in between 1.1and 1.10.

但是,这里1.2应该介于1.1和之间1.10

回答by Jan van der Vegt

You can use:

您可以使用:

list1 = sorted(list1)

If it is in the second format (as a string) you can use the key parameter to convert it into floats by using:

如果它是第二种格式(作为字符串),您可以使用 key 参数将其转换为浮点数:

list1 = sorted(list1, key=float)

The key parameter expects a function that will transform the values before sorting using the transformed values, but keeping the original values

关键参数需要一个函数,该函数将在使用转换后的值排序之前转换值,但保留原始值

回答by Strik3r

You can sort any list in two ways.

您可以通过两种方式对任何列表进行排序。

  • Using the sortedmethod :: In this the sorted method will return a sorted list but the actual list remains the same

    x=[1,2,3.1,4.5,2.3]
    y = sorted(x)
    y = sorted(x,key=float) #in case if the values were there as string.
    

    In this case x still remains as [1,2,3.1,4.5,2.3], where as the sorted list i.e [1,2,2.3,3.1,4.5] will be returned and in this case will be assigned to y.

  • Using the sortmethod call provided for lists :: in this the sort method call will sort the actual list

    x=[1,2,3.1,4.5,2.3]
    x.sort()
    

    In this case x will be sorted , hence if you try to print x it will be like [1,2,2.3,3.1,4.5].

  • 使用sorted方法 :: 在这个 sorted 方法将返回一个已排序的列表,但实际列表保持不变

    x=[1,2,3.1,4.5,2.3]
    y = sorted(x)
    y = sorted(x,key=float) #in case if the values were there as string.
    

    在这种情况下,x 仍然保持为 [1,2,3.1,4.5,2.3],其中排序列表即 [1,2,2.3,3.1,4.5] 将被返回,在这种情况下将被分配给 y。

  • 使用为列表提供的sort方法调用 :: 在此 sort 方法调用将对实际列表进行排序

    x=[1,2,3.1,4.5,2.3]
    x.sort()
    

    在这种情况下 x 将被排序,因此如果您尝试打印 x 它将像 [1,2,2.3,3.1,4.5]。

You can use any of these methods according to your requirement.

您可以根据需要使用这些方法中的任何一种。

Hope it helps. Happy Coding :)

希望能帮助到你。快乐编码:)

回答by Sebastian Wozny

Just use sorted:

只需使用sorted

sorted(list1, key=float)

This will convert the element to floatbefore comparing, so it will work for both a list of strings or a list of floats (or ints, for what it's worth).

这将float在比较之前将元素转换为,因此它适用于字符串列表或浮点数列表(或整数,对于它的价值)。

回答by RaunakSabhani

Use the sort() method.

使用 sort() 方法。

list1.sort()
print(list1)