人生苦短,我用Python

前言

前几日代写留学生作业时遇到了两道很有意思的Python编程题,在此做一记录,主要涉及到一些Python的高级特性:生成器、lambda表达式。

Question 1

题意

原文:
Consider the following function

Code
1
2
3
4
5
6
7
8
9
10
# listOfNumbers is a list of only numbers
#
def processList(listOfNumbers):
    result = []
    for i in listOfNumbers:
        if i<0 == 0:
            result.append(ii)
        else:
            result.append((ii)+1)
    return result

First, study and test processList(listOfNumbers) to determine what it does Then rewrite its body so that it accomplishes the same task with a one-line list comprehension. Thus, the resulting function will have exactly two lines, the def line and a return line containing a list comprehension expression.
翻译:
将给出的一个函数变成两行

分析

函数的功能是:遍历listofNumbers中的每一个元素,如果大于等于0,将其变成自己乘以自己,否则变为自己乘自己后再加一。
因此,我们可以用到Python中的三元表达式,下面这两种方式是等价的。
value = true if condition else false

Code
1
2
3
4
if condition:
    value = true
else:
    value = false

参考答案

Code
1
2
def processList(listOfNumbers):
    return [i  i if i >= 0 else i  i + 1 for i in listOfNumbers]

Question 2

题意

原文:
Implement function processList2(inputList, specialItem, ignoreItems) that returns a new list that contains all the items of inputList (and in the original order) except 1) any that appear in the list ignoreItems, and 2) occurrences of specialItem (if specialItem is not in ignoreItems) should become the string “special” in the new list. Use a one-line list cluoomprehension to construct the new list. Thus, again, the function will have exactly two lines, the def line and a return line containing a list comprehension expression. For example,

processList2([1,2,3,4], 4, [3])
[1, 2, ‘special’]
processList2([1,2,3,4,True,’dog’], 4, [3,5,4])
[1, 2, True, ‘dog’]
processList2([1,1,2,2], 1, [2])
[‘special’, ‘special’]


翻译:
实现函数processList2(inputList, specialItem, ignoreItems),对inputList中的元素进行处理,返回一个新列表,但需满足以下两个要求:
  1. 不能是出现在ignoreItems中的元素
  2. 对于与specialItem相同的元素(不能在ignoreItems中出现),将其变成”special”。

分析

  1. 首先将不在ignoreItems中的那些元素筛出来[x for x in inputList if x not in ignoreItems]list(filter(lambda x:False if x in ignoreItems else True,inputList))
  2. 然后遍历上述列表中的元素,将specialItem变成”special”,["special" if x == specialItem else x for x in xxx]

参考答案

Code
1
2
3
4
def processList2(inputList, specialItem, ignoreItems):
    ## two method
    return [“special” if x == specialItem else x for x in list(filter(lambda x:False if x in ignoreItems else True,inputList))]
    return[“special” if x == specialItem else x for x in [x for x in inputList if x not in ignoreItems]]

参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值