更短的"lambda"关键字替代方法?
Python是关于简单易读的代码的.它的版本比以前更好,我是忠实的粉丝!但是,每次必须定义lambda时键入l
a
m
b
d
a
都是不好玩的(您可能会不同意).
问题是,这6个字符l
a
m
b
d
a
使我的语句更长,特别是当我在map
和filter
s中嵌套几个lambda时.
我嵌套的数量不超过2或3,因为它占用了python的可读性!
Python is about simple and readable code. It got better over the versions and I am a huge fan! However, typing l
a
m
b
d
a
every time I have to define a lambda is not fun (you may disagree).
The problem is, these 6 characters l
a
m
b
d
a
make my statements longer, especially when I nest a couple of lambdas inside map
s and filter
s.
I am not nesting more than 2 or three, because it takes away the readability of python!
# How to rename/alias a keyword to a nicer one?
lines = map(lmd x: x.strip(), sys.stdin)
# OR, better yet, how to define my own operator like -> in python?
lines = map(x -> x.strip(), sys.stdin)
# Or may be :: operator is pythonic
lines = map(x :: x.strip(), sys.stdin)
# INSTEAD of this ugly one. Taking out this is my goal!
lines = map(lambda x: x.strip(), sys.stdin)
我很高兴添加这样的导入:
I am happy to add import like this:
from myfuture import lmd_as_lambda
# OR
from myfuture import lambda_operator
好消息是:您根本不需要使用map
或filter
,您可以使用列表理解(急切),因此完全避免了lambda
.
The good news is: You don't need to use map
or filter
at all, you can use generator expressions (lazy) or list comprehensions (eager) instead and thus avoid lambda
s completely.
所以代替:
lines = map(lambda x: x.strip(), sys.stdin)
只需使用:
# You can use either of those in Python 2 and 3, but map has changed between
# Python 2 and Python 3 so I'll present both equivalents:
lines = (x.strip() for x in sys.stdin) # generator expression (Python 3 map equivalent)
lines = [x.strip() for x in sys.stdin] # list comprehension (Python 2 map equivalent)
如果您使用理解力,它可能也会更快.在map
或filter
中使用时,实际上很少有函数会更快-使用lambda
时,会有更多的反模式(而且很慢).
It's probably also faster if you use comprehensions. Very few functions are actually faster when used in map
or filter
- and using a lambda
there is more of an anti-pattern (and slow).
该问题仅包含map
的示例,但是您也可以替换filter
.例如,如果您要filter
排除奇数:
The question only contained an example for map
, but you can also replace filter
. For example if you want to filter
out odd numbers:
filter(lambda x: x%2==0, whatever)
您可以改为使用条件理解:
You can use a conditional comprehension instead:
(x for x in whatever if x%2==0)
[x for x in whatever if x%2==0]
您甚至可以将map
和filter
组合在一起:
You could even combine a map
and filter
in one comprehension:
(x*2 for x in whatever if x%2==0)
只需考虑map
和filter
的外观:
map(lambda x: x*2, filter(lambda x: x%2==0, whatever))
注意:这并不意味着lambda
没有用!在很多地方lambda
非常方便.考虑sorted
的 key
参数(对于min
同样和max
)或 functools.reduce
(但最好远离该函数,大多数情况下通常需要一个谓词函数的for
-loop或itertools
可读性更好: itertools.groupby
和
Note: That doesn't mean lambda
isn't useful! There are lots of places where lambda
s are very handy. Consider the key
argument for sorted
(and likewise for min
and max
) or functools.reduce
(but better keep away from that function, most of the times a normal for
-loop is more readable) or itertools
that require a predicate function: itertools.accumulate
, itertools.dropwhile
, itertools.groupby
and itertools.takewhile
. Just to name a few examples where a lambda
could be useful, there are probably lots of other places as well.