How to sort with lambda in Python

前端 未结 4 1497
感情败类
感情败类 2020-11-30 19:41

In Python, I am trying to sort by date with lambda. I can\'t understand my error message. The message is:

() takes exactly 1 argument (2 given         


        
相关标签:
4条回答
  • 2020-11-30 20:11
    lst = [('candy','30','100'), ('apple','10','200'), ('baby','20','300')]
    lst.sort(key=lambda x:x[1])
    print(lst)
    

    It will print as following:

    [('apple', '10', '200'), ('baby', '20', '300'), ('candy', '30', '100')]
    
    0 讨论(0)
  • 2020-11-30 20:26

    Python lists have two built-in ways to sort data:

    sort() — A method that modifies the list in-place
    sorted() — A built-in function that builds a new sorted list from an iterable
    

    Based on your requirement you can choose among these two:

    if you want to keep original list ,you can use sorted function or if you don't need original list you can use sort function.

    Before going on sort or sorted ,we need to understand lambda.

    A lambda is an anonymous function and an anonymous function is a function that is defined without a name, this post seems to explain it pretty nicely.

    https://www.programiz.com/python-programming/anonymous-function

    Lambda functions are nice for calling in-line because they only have one expression which is evaluated and returned. They syntax for a lambda is:

    lambda arguments: expression

    let's see how to use sorted function:

    student_tuples = [('john', 'A', 15),('jane', 'B', 12),('dave', 'B', 10),]
    sorted(student_tuples, key=lambda student: student[2]) 
    

    output: [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

    Here we can see list student_tuples having tuples is sorted based on key parameter provided that is student[2].

    0 讨论(0)
  • 2020-11-30 20:28

    You're trying to use key functions with lambda functions.

    Python and other languages like C# or F# use lambda functions.

    Also, when it comes to key functions and according to the documentation

    Both list.sort() and sorted() have a key parameter to specify a function to be called on each list element prior to making comparisons.

    ...

    The value of the key parameter should be a function that takes a single argument and returns a key to use for sorting purposes. This technique is fast because the key function is called exactly once for each input record.

    So, key functions have a parameter key and it can indeed receive a lambda function.

    In Real Python there's a nice example of its usage. Let's say you have the following list

    ids = ['id1', 'id100', 'id2', 'id22', 'id3', 'id30']
    

    and want to sort through its "integers". Then, you'd do something like

    sorted_ids = sorted(ids, key=lambda x: int(x[2:])) # Integer sort
    

    and printing it would give

    ['id1', 'id2', 'id3', 'id22', 'id30', 'id100']
    

    In your particular case, you're only missing to write key= before lambda. So, you'd want to use the following

    a = sorted(a, key=lambda x: x.modified, reverse=True)
    
    0 讨论(0)
  • 2020-11-30 20:36

    Use

    a = sorted(a, key=lambda x: x.modified, reverse=True)
    #             ^^^^
    

    On Python 2.x, the sorted function takes its arguments in this order:

    sorted(iterable, cmp=None, key=None, reverse=False)
    

    so without the key=, the function you pass in will be considered a cmp function which takes 2 arguments.

    0 讨论(0)
提交回复
热议问题