我在 python 中使用地图函数有困难

I have difficulty using map function in python

我对 python 很陌生。
我研究了我的问题,但无法得到例外的答案。
我在这里不明白的是 myfunc 是如何被调用的,因为它没有像 myfunc() 这样的参数,参数 (n) 如何接受两个参数(苹果和香蕉)?

    def myfunc(n):
        return len(n)

    x = list(map(myfunc,('apple', 'banana')))
    print(x)
   
    output:
    [5,6]

map(fun, iterable)fun 函数应用到可迭代对象(例如列表)中的每个元素,并 returns 应用列表中的每个输出。

函数 myfunc 没有参数的原因是你应该把它看作是 map 函数的一个参数。

试着想地图,你的例子是这样的:

[5, 6] = [myfunc('apple'), myfunc('banana')]

在内部,map 函数正在做类似的事情:

def map(myfunc, iterable):
    returns = []
    for i in iterable:
        returns.append(myfunc(i))
    return returns

Map 实际上并不接受函数,它接受对函数的引用。引用是在没有括号的情况下传递的,因此您不能手动提供参数,map 会为您完成。例如。 如果我们想将任何东西转换为整数,我们将使用

int(50.65)

但是 map 只获取函数的引用。

map(int, input().split())