题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
import sys for line in sys.stdin: a = line.rstrip() list_count = [] list_count.append(sum(i.isalpha() for i in a)) list_count.append(a.count(" ")) list_count.append(sum(i.isdigit() for i in a)) list_count.append(len(a)-sum(list_count)) #print("\n".join(map((list_count,x,str(x))))) print("\n".join(map(lambda x: str(x), list_count)))
map() 函数是Python内置的高阶函数之一,它接受一个函数和一个可迭代对象作为参数,然后对可迭代对象中的每个元素应用该函数,返回一个将函数应用后的结果组成的迭代器。
map() 函数的基本语法如下:
map(function, iterable)
function:表示要对可迭代对象中的每个元素应用的函数。
iterable:表示一个或多个可迭代对象,如列表、元组等。
map() 函数会将function应用于iterable中的每个元素,返回一个包含结果的迭代器。如果function参数为None,则map()函数会直接将iterable中的元素转换为迭代器。
在您提供的代码中,使用了map()函数将一个匿名函数 lambda x: str(x) 应用到 list_count 中的每个元素上,将每个元素转换为字符串类型。然后使用 "\n".join() 方法将转换后的字符串元素用换行符连接起来,最终输出一个带有换行符的字符串。