【问题标题】:What does this one liner means in lambda function in Python?在 Python 的 lambda 函数中,这一行是什么意思?
【发布时间】:2020-09-21 18:26:33
【问题描述】:

我被困在这个 lambda 单线中。这个单行用于对列表中的字符串进行排序,但不知道这实际上是如何工作的?请帮助我理解这一点。 这是以下功能的动机:

sort_item_list_by_author():接受新的图书列表并返回按作者姓名字母顺序排序的图书。注意:按字母顺序排列作者姓名时,忽略包括空格在内的特殊字符(如果有)。

    def sort_item_list_by_author(self,new_item_list):
       new_item_list.sort(key=lambda x:''.join(e for e in x.get_author_name() if e.isalnum())) #problem causing line

    item11=Item("Broken Wing","Sarojini Naidu",2012)
    item12=Item("Guide","R.K.Narayanan",2001)
    item13=Item("Indian Summers","John Mathews",2001)
    item14=Item("Innocent in Death","J.D.Robb",2010)
    item15=Item("Life of Pi","Yann Martel",2010 )
    item16=Item("Sustainability","Johny",2016)
    item17=Item("Look Ahead","E.M.Freddy",2012 )

    new_item_list=[item11,item12,item13,item14,item15,item16,item17]
    new_item_list_sorted=library.sort_item_list_by_author(new_item_list)

【问题讨论】:

  • 这条线的哪个方面让您感到困惑?
  • 上面一行和这一行有什么区别-new_item_list.sort(key=lambda x:x.get_author_name())?
  • @user2357112supportsMonica 我无法理解加入

标签: python python-3.x list sorting lambda


【解决方案1】:

你说的 lambda 是排序函数的关键,这意味着你用它来比较列表中的值。

lambda x:''.join(e for e in x.get_author_name() if e.isalnum()

所以对于列表的每个元素,都会调用这个 lambda 函数。这里x代表lambda函数的参数(这里是列表项)。 现在 lambda 函数的主体本身非常简单,我们获取作者姓名,并逐个字符循环遍历它,并仅选择那些字母数字字符(意味着我们忽略空格和所有字符),然后我们加入一个字符串中的所有获得的字符。

使用 lambda 函数,对于列表中的所有项目,创建这样的字符串(作者姓名的字母数字部分),用于对列表进行排序。 lambda 转换为如下内容:

x = item() #fill the fields here
s = ''
for e in x.get_author_name():
    if e.isalnum():
       s += e

【讨论】:

  • 但是为什么我们会看到字母数字,因为我们可以通过获取每个列表项的作者姓名来简单地对作者姓名进行排序?
  • 因为没有字母数字条件,您将在 BWriter 前面有 A. Writer ,这不应该发生。所以我们有条件忽略空格、点等。
  • 所以你的意思是我们必须忽略点以及空格?好的,如果是的话,你能把这个单行代码分解成初学者级别的代码吗?
  • 您在问题中提到我们必须忽略特殊字符。
  • 好的,请把这个单行代码分解成更易理解的代码,供像我们这样的初学者使用。
【解决方案2】:

在 Python 中,lambda 是用于定义匿名函数(没有名称的函数)的关键字,这就是它们被称为 lambda 函数的原因。让我们看一个例子。

>>> addition=lambda x1,x2:x1+x2  
>>> subtraction=lambda x1,x2:x1-x2
>>> addition(10,20)
30
>>> subtraction(10,20)
-10
>>> #there is another way also
>>>(lambda num1, num2: num1+num2)(10,20)
30

现在假设我们有一个项目列表(整数和带有数字内容的字符串)

numbers = [1,"2", "5", 3, 4, "8", "-1", "-11"]

现在我使用 sorted 函数对其进行排序,您也可以使用 sort,但我们知道 sort 函数会更改原始数据,但 sorted 返回一个新的排序列表

>>> numbers = [1,"2", "5", 3, 4, "8", "-1", "-11"]
>>> sorted(numbers)
[1, 3, 4, '-1', '-11', '2', '5', '8']
>>>

但这不是预期的答案

['-11', '-1', 1, '2', 3, 4, '5', '8']

所以这里我们习惯用关键字作为参数

>>> sorted(numbers, key=int)
['-11', '-1', 1, '2', 3, 4, '5', '8']
>>> sorted(['There', 'are','some', 'sort', 'words'], key=lambda word: word.lower())
['are', 'some', 'sort', 'There', 'words']

与此相同

sorted(['There', 'are','some', 'sort', 'words'],key=str.lower)

根据https://docs.python.org/2/library/functions.html?highlight=sorted#sorted,key指定一个参数的函数,用于从每个列表元素中提取比较键 key=str.lower 默认值为None(直接比较元素)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 2021-01-19
    • 2017-02-12
    • 2021-11-20
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多