[英]How to rewrite this lambda function to regular function
result = map(lambda x: dictionary[x], mylist)
I am learning lambda expressions and the concept seems to be little confusing.我正在学习 lambda 表达式,这个概念似乎有点混乱。 I am trying to understand what this lambda function is supposed to do and rewrite as a separate function.
我试图了解这个 lambda function 应该做什么并将其重写为单独的 function。 My understanding is that it takes an item from the list and it then get a value from the dictionary...
我的理解是它从列表中获取一个项目,然后从字典中获取一个值......
Below is how I attempted to rewrite the lambda part下面是我如何尝试重写 lambda 部分
result = list()
for x in mylist:
value = dictionary[x]
result.append(value)
Read this Why are Python lambdas useful?阅读这篇文章为什么 Python lambdas 有用?
dictionary = {'a':5,'b':6,'c':7}
mylist = ['a','c']
def f1(x):
return dictionary[x]
#lambda x: dictionary[x]
result= map(f1, mylist)
print(list(result))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.