简体   繁体   English

如何按升序对长列表进行排序

[英]how to sort a long list by ascending order

                print(addition)
                decimal = sum(addition) / len(addition)
                d = decimal * 100
                percentage = float("{0:.2f}".format(d))
                print(percentage)
                list_name_second.append({'Name': x, 'percentage': float(percentage)})
                list_name.append(x)
                addition.clear()

    i = i + 1
if i == num_lines:
    def sort_percentages(list_name_second):
        print(list_name_second.sort('percentage'))

    print(sort_percentages(list_name_second), end='\n\n')

the code above is within a loop (apart from the bottom part) it collects percentages and stores then in a list that looks like this:上面的代码在一个循环中(除了底部)它收集百分比然后存储在一个看起来像这样的列表中:

[{'Name': 'Malcolm', 'percentage': '50.0'}]

I want to be able to sort the list using the percentages but I don't know how to do this, please help我希望能够使用百分比对列表进行排序,但我不知道该怎么做,请帮忙

also the print functions within the calculations were for troubleshooting, dont pay attention to them计算中的打印功能也用于故障排除,不要关注它们

You can use python's built-in functions sort() or sorted() where sort() method will sort the list in-place, and sorted() will return a new sorted list in case you don't need the original list.您可以使用 python 的内置函数sort()sorted()其中sort()方法将对列表进行就地排序,而sorted()将返回一个新的排序列表,以防您不需要原始列表。 both sort() and sorted() have a key parameter that specify a callable which will be called on each list's element for comparisons. sort()sorted()都有一个key参数,该参数指定一个可调用对象,该可调用对象将在每个列表的元素上调用以进行比较。 for more details, see docs有关更多详细信息,请参阅文档

if we say that your list for example look like this:例如,如果我们说您的列表如下所示:

>>> list_name_second = [
    {'Name': 'Malcolm', 'percentage': 50.0},
    {'Name': 'Sam', 'percentage': 30.0},
    {'Name': 'Ru', 'percentage': 100.0},
    {'Name': 'Kam', 'percentage': 10.0},
    {'Name': 'Joe', 'percentage': 20.0}
   ]

so, you can sort your list in-place based on percentage key of dict list element using sort(key=callable) like follows:因此,您可以使用sort(key=callable)根据dict列表元素的percentage键对列表in-place排序,如下所示:

>>> list_name_second.sort(key=lambda d: d['percentage'])
>>> list_name_second
[{'Name': 'Kam', 'percentage': 10.0}, {'Name': 'Joe', 'percentage': 20.0}, {'Name': 'Sam', 'percentage': 30.0}, {'Name': 'Malcolm', 'percentage': 50.0}, {'Name': 'Ru', 'percentage': 100.0}]

or, you can use sorted(key=callable) which it will return a new sorted list, like follows:或者,您可以使用sorted(key=callable)它将返回一个新的排序列表,如下所示:

>>> sorted(list_name_second, key=lambda d: d['percentage'])
[{'Name': 'Kam', 'percentage': 10.0}, {'Name': 'Joe', 'percentage': 20.0}, {'Name': 'Sam', 'percentage': 30.0}, {'Name': 'Malcolm', 'percentage': 50.0}, {'Name': 'Ru', 'percentage': 100.0}]

here, you are passing a callable lambda function to be applied on each element of the list, which its extract the 'percentage' key's value from the dict and return it to sort or sorted built-in functions to be used as reference for sorting process.在这里,您传递了一个可调用的 lambda function 以应用于列表的每个元素,它从dict中提取'percentage'键的值并将其返回给sortsorted的内置函数,用作排序过程的参考.

Note: if your elements in the list look like this: [{'Name': 'Malcolm', 'percentage': '50.0'}] , where 'percentage' 's value is a string, you have to cast its value to compared as a float, like this: Note:如果列表中的元素如下所示: [{'Name': 'Malcolm', 'percentage': '50.0'}] ,其中'percentage'的值是字符串,则必须将其值转换为比较为浮点数,如下所示:

>>> list_name_second = [
    {'Name': 'Malcolm', 'percentage': '50.0'},
    {'Name': 'Sam', 'percentage': '30.0'},
    {'Name': 'Ru', 'percentage': '100.0'},
    {'Name': 'Kam', 'percentage': '10.0'},
    {'Name': 'Joe', 'percentage': '20.0'}
    ]
>>> sorted(list_name_second, key=lambda d: float(d['percentage']))
[{'Name': 'Kam', 'percentage': '10.0'}, {'Name': 'Joe', 'percentage': '20.0'}, {'Name': 'Sam', 'percentage': '30.0'}, {'Name': 'Malcolm', 'percentage': '50.0'}, {'Name': 'Ru', 'percentage': '100.0'}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM