map是python内置的高阶函数,其接受一个函数f和一个list,通过函数f作用于每一个元素,并返回一个map迭代器。map使用举例如下:
# ord 根据字符获取对应的ascii码
# chr 根据ascii码获取对应的字符
from collections.abc import Iterator
# 判断以下单词是否为按照字母升序排列
test_list = ["effort", "circle", "yearly", "woolen", "accept", "lurker",
"island", "faucet", "glossy", "evader"]
def is_ordered(word):
for i in range(0, len(word) - 1):
if ord(word[i]) > ord(word[i + 1]):
return False
return True
res = map(is_ordered, test_list)
print(isinstance(res, Iterator))
print(list(res))