In Python that’s a line:
sorted(i[3] for i in x if i[0] == y)
Forget the use of "map" in general it is more complicated than the "Generator Expressions" and "list comprehensions" that we have embedded in the language -
these fancy names are for use of the "for" command in an expression inline, as I did above - and allow, without using the functions "map", "filter", or the keyword "lambda" the mapping and filtering of any data set:
Basically, you write it "<expressão> for item in <iterador> if <expressão-de-filtro>"
anywhere where the language expects an iterator - it will give you all the items of the iterator, and for each, it will process the < Expressão>
if the expressão-de-filtro
is true.
That is, the initial expression plays the role of "map", and the expression after "if" plays the role of "filter".
If you want the final result in a list, put that syntax in square brackets.
([<expressão> for item in <iterador> if <expressão-de-filtro>]
) - in case, as you want the results ordered, instead I passed the iterator to a call to sorted
that returns an object list
already ordained.
The expression I gave as an answer above would be the equivalent of:
map((lambda i: i[3]), filter((lambda i: i[0] == y), x) )
(that is, the iterator returned by the call to filter
is the sequence parameter for calling the map
- is much harder to read)
(also note that it is best to protect the Lambda with parentheses to avoid ambiguity, for human readers, whether the "," indicates the end of the lambda, or indicates that the lambda will return a tuple, rather than a single result)
yes you’re right, I thought I could put the whole code on one line, but I see that not... thank you!
– CSAnimor