python小知识---map

mappython的内置函数,该函数接收两个参数,第一个参数为一个函数f,第二个参数s是一个序列。map会将传入的函数f应用到每一个s中的元素上。

text = [
"It is said that the true nature of being is veiled. ",
"The labor of words, the expression of art, ",
"the seemingly ceaseless buzz that is human ",
"thought all have in common the need to get at what really is so." ,
"The hope to draw close to and possess the truth of being can be a feverish one. ",
"In some cases it can even be fatal, ",
"if pleasure is one's truth and its attainment more important than life itself. ",
"In other lives, though, the search for what is truthful gives life."
]

mark = map(lambda s: (True, s) if 'if' in s else (False, s), text);

print(type(mark))
print(mark)
print(list(mark))

输出结果:
image

可以看到map的返回值为<class 'map'>,如果直接输出这个返回值会打印出它的内存地址,想要正确输出需要转化成list

posted @ 2022-04-01 18:45  yuzuki_n  阅读(108)  评论(0)    收藏  举报