相关疑难解决方法(0)

星级算子是什么意思?

*运算符在Python 中的含义是什么,例如在代码中zip(*x)f(**k)

  1. 如何在解释器内部处理?
  2. 它会影响性能吗?是快还是慢?
  3. 什么时候有用,什么时候不用?
  4. 它应该用于功能声明还是通话中?

python syntax parameter-passing argument-unpacking iterable-unpacking

559
推荐指数
5
解决办法
17万
查看次数

如何在Python 3中使用filter,map和reduce

filter,map并且reduce在Python 2中完美地工作.这是一个例子:

>>> def f(x):
        return x % 2 != 0 and x % 3 != 0
>>> filter(f, range(2, 25))
[5, 7, 11, 13, 17, 19, 23]

>>> def cube(x):
        return x*x*x
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

>>> def add(x,y):
        return x+y
>>> reduce(add, range(1, 11))
55
Run Code Online (Sandbox Code Playgroud)

但是在Python 3中,我收到以下输出:

>>> filter(f, range(2, 25))
<filter object at 0x0000000002C14908>

>>> map(cube, range(1, 11))
<map object at 0x0000000002C82B70> …
Run Code Online (Sandbox Code Playgroud)

python reduce functional-programming filter python-3.x

292
推荐指数
5
解决办法
25万
查看次数