简体   繁体   English

在python中使用map函数

[英]Using map function in python

If you have the following code, how exactly is it following the documentation: map(function, iterable,...) ? 如果您具有以下代码,那么其遵循文档的map(function, iterable,...)如何: map(function, iterable,...)

x = sorted(map(int, dat[0].split()))

Is int a function and if so, why isn't it expressed as such? int是一个函数吗?如果是,为什么不这样表示呢?

int是一个构造函数,因此可以调用,因此可以将其与map

In your case dat[0] is as string, and split() generates a list of strings, by splitting the input string at whitespaces. 在您的情况下, dat[0]作为字符串, split()通过在空白处分割输入字符串来生成字符串列表。

Eg 例如

"1 11".split()

returns 退货

["1", "11"]

The map function has two input arguments: map函数具有两个输入参数:

  1. The first argument is something which can be called (in Python you say it is a callable ), eg a function. 第一个参数是可以调用的东西(在Python中,您说它是可调用的 ),例如函数。 int is not realy a function but such a thing (Python slang: it is an object ). int实际上不是函数,而是这样的东西 (Python Python语:它是一个object )。 Eg int("3") return 3 . 例如int("3")返回3 So int when applied to a string tries to convert this string to an integer, and gives the integer value back. 因此,将int应用于字符串时,它会尝试将此字符串转换为整数,并返回整数值。

  2. The second argument is something you can iterate over , in your case it is a list. 第二个参数是您可以迭代的东西,在您的情况下,它是一个列表。

If you then call the map function, the first argument is applied to all elements from the second argument. 如果然后调用map函数,则第一个参数将应用于第二个参数中的所有元素。 So 所以

map(int, ["1", "11"])

returns 退货

[1, 11]

If you combine what I explained you understand that 如果结合我的解释,您将了解

map(int, "1 11".split())

returns 退货

[1, 11]

When you ask "why isn't it expressed as such" I suppose you mean, why doesn't it have brackets like a function? 当您问“为什么不这样表示”时,我想您的意思是,为什么它没有像函数这样的括号? The answer is that if you put the brackets in then you get what the function does instead of the function itself. 答案是,如果放在方括号中,则可以得到函数的功能,而不是函数本身。 Compare what happens when you enter int() versus int . 比较当您输入int()int时发生的情况。

Think of it like this 这样想

def map( function, iterable ):
    return ( function(x) for x in iterable )

In x = sorted(map(int, dat[0].split())) the function, int , is being named , not evaluated . x = sorted(map(int, dat[0].split())) ,函数int命名 ,而不是求值 This code provides a function object to the map function. 这段代码为map函数提供了一个函数对象。 The map function will evaluate the given function. map功能将评估给定的功能。

The syntax of map in the simplest form is: map的最简单形式为:

map(func, sequence)

It will apply the function "func" on each element of the sequence and return the sequence. 它将对序列的每个元素应用函数“ func”并返回序列。 Just in case you don't know, int() is a function. 万一您不知道,int()是一个函数。

>>> int(2.34)
2
>>> int("2")
2
>>> a = ["1", "101", "111"]
>>> map(int, a)
[1, 101, 111]

Now, I will give you my implementation of map(). 现在,我将为您提供map()的实现。

>>> for index in range(len(a)):
...     a[index] = int(a[index])
... 
>>> a
[1, 101, 111]

If you have understood the concept, let's take a step further. 如果您已经理解了这个概念,那就让我们更进一步。 We converted the strings to int using base 10 (which is the default base of int() ). 我们使用10为底(这是int()的默认底数)将字符串转换为int。 What if you want to convert it using base 2? 如果要使用基数2进行转换怎么办?

>>> a = ["1", "101", "111"]
>>> for index in range(len(a)):
...     a[index] = int(a[index], 2) # We have an additional parameter to int()
...     
>>> a
[1, 5, 7]

To get the same result using map(), we will use a lambda function 为了使用map()获得相同的结果,我们将使用lambda函数

>>> a = ["1", "101", "111"]
>>> map(lambda x: int(x, 2), a)
[1, 5, 7]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM