0

If you have the following code, how exactly is it following the documentation: map(function, iterable,...)?

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

Is int a function and if so, why isn't it expressed as such?

2
  • 1
    I'm assuming you at least get the idea of what map is supposed to do when you ask this question. In that case, if you're surprised that int is usable as the function argument of map, why don't you just try using it as a function of one argument and see what happens? i.e. try typing int(FOO) in a Python interpreter for various different kinds of FOO. A little playing around in the interpreter is a very good way to answer these kinds of simple questions, and almost invariably quicker than typing up a question for SO. Then you can ask a more targeted question if you need to afterwards. Commented Sep 29, 2011 at 8:42
  • "how exactly is it following the documentation"? Please provide the documentation that actually confuses you. We don't know what you've read (or haven't read). We can't really guess what you mean by this question. Please update it to include a link to the documentation which confuses you. Commented Sep 29, 2011 at 10:44

5 Answers 5

3

int is a constructor so it's callable so you can use it with map

Sign up to request clarification or add additional context in comments.

2 Comments

sorry, what do you mean? What is it a constructor of?
Please refer to docs: The constructors int(), long(), float(), and complex() can be used to produce numbers of a specific type.
3

In your case dat[0] is as string, and split() generates a list of strings, by splitting the input string at whitespaces.

Eg

"1 11".split()

returns

["1", "11"]

The map function has two input arguments:

  1. The first argument is something which can be called (in Python you say it is a callable), eg a function. int is not realy a function but such a thing (Python slang: it is an object). Eg int("3") return 3. So int when applied to a string tries to convert this string to an integer, and gives the integer value back.

  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. 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]

Comments

1

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.

2 Comments

I discover now int() returns 0 while in docs int(x[, base]) -> integer does not suppose a default value for x. Questionable thing...
Hmm, maybe. But it would be wrong if int() didn't produce something of type int: list() produces a list, str() produces a string, float() produces a float, and so on. And what other integer apart from 0 could you expect int() to produce? Another interesting one is bool(), which produces... no, I'll let you guess.
1

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. This code provides a function object to the map function. The map function will evaluate the given function.

Comments

0

The syntax of map in the simplest form is:

map(func, sequence)

It will apply the function "func" on each element of the sequence and return the sequence. Just in case you don't know, int() is a function.

>>> 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().

>>> 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() ). What if you want to convert it using base 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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.