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?
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:
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.
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]
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() returns 0 while in docs int(x[, base]) -> integer does not suppose a default value for x. Questionable thing...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.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.
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]
mapis supposed to do when you ask this question. In that case, if you're surprised thatintis usable as the function argument ofmap, why don't you just try using it as a function of one argument and see what happens? i.e. try typingint(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.