What does this mean: key=lambda x: x[1] ?

前端 未结 6 2020
离开以前
离开以前 2021-02-05 06:17

I see it used in sorting, but what do the individual components of this line of code actually mean?

key=lambda x: x[1]

What\'s lambda

相关标签:
6条回答
  • 2021-02-05 06:52

    One more example of usage sorted() function with key=lambda. Let's consider you have a list of tuples. In each tuple you have a brand, model and weight of the car and you want to sort this list of tuples by brand, model or weight. You can do it with lambda.

    cars = [('citroen', 'xsara', 1100), ('lincoln', 'navigator', 2000), ('bmw', 'x5', 1700)]
    
    print(sorted(cars, key=lambda car: car[0]))
    print(sorted(cars, key=lambda car: car[1]))
    print(sorted(cars, key=lambda car: car[2]))
    

    Results:

    [('bmw', 'x5', 1700), ('citroen', 'xsara', 1100), ('lincoln', 'navigator', 2000)]
    [('lincoln', 'navigator', 2000), ('bmw', 'x5', 1700), ('citroen', 'xsara', 1100)]
    [('citroen', 'xsara', 1100), ('bmw', 'x5', 1700), ('lincoln', 'navigator', 2000)]
    
    0 讨论(0)
  • 2021-02-05 06:58
    distances.sort(key=lambda x: x[1])
    

    This is the function. And here x is the list, in which we are adding x[1] i.e 2nd element of list to the sort function. So, basically we are adding every list's 2nd element (i.e x[1]) to the sort function. I hope you understand this.

    0 讨论(0)
  • 2021-02-05 07:00
    student_tuples = [
        ('john', 'A', 15),
        ('jane', 'B', 12),
        ('dave', 'B', 10),]
    sorted(student_tuples, key=lambda student: student[2])   # sort by age
    >>>>[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
    

    from official documentation

    0 讨论(0)
  • 2021-02-05 07:01

    lambda effectively creates an inline function. For example, you can rewrite this example:

    max(gs_clf.grid_scores_, key=lambda x: x[1])
    

    Using a named function:

    def element_1(x):
        return x[1]
    
    max(gs_clf.grid_scores_, key=element_1)
    

    In this case, max() will return the element in that array whose second element (x[1]) is larger than all of the other elements' second elements. Another way of phrasing it is as the function call implies: return the max element, using x[1] as the key.

    0 讨论(0)
  • 2021-02-05 07:10

    From a reference for Python 3.7 (https://docs.python.org/3/howto/sorting.html), The key is a parameter of list.sort() and sorted(). The first built-in function modifies a list in place while the latter accepts and return iterable.

    The key parameter can be defined as a function to be called on each element of list/iterable before comparison and sort, respectively. In this case, the inline function lambda x: x[1] is defined as a value of the key parameter. The lambda function takes input x return x[1] which is the second element of x.

    Supposed

    mylist = [[7, 8], [1, 2, 3], [2, 5, 6]]
    # list(map(lambda x: x[1], mylist)) returns [8, 2 ,5]
    
    mylistSort = sorted(mylist, key = lambda x: x[1])
    # will sort the nested list based on the result of the lambda function 
    

    Can you guess what the result? mylistSort is then [[1,2,3], [2,5,6], [7,8]] from the sorted sequence of [8,2,5] which is [2,5,8].

    The max() in your example is applied to just get the max value from the outcome of the sort function.

    I hope this post is helpful.

    0 讨论(0)
  • 2021-02-05 07:12

    lambda signifies an anonymous function. In this case, this function takes the single argument x and returns x[1] (i.e. the item at index 1 in x).

    Now, sort(mylist, key=lambda x: x[1]) sorts mylist based on the value of key as applied to each element of the list. Similarly, max(gs_clf.grid_scores_, key=lambda x: x[1]) returns the maximum value of gs_clf.grid_scores_ with respect to whatever is returned by key for each element.

    I should also point out that this particular function is already included in one of the libraries: operator. Specifically, operator.itemgetter(1) is equivalent to your key.

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