Re: [Tutor] list comprehension equivalent to map(function, list item)
On 12/14/2013 10:12 AM, Bo Morris wrote: Thank you for your assistance. Based on your direction, I figured it out. *This... * def add(number): print 1 + int(number) x = ['2', '4', '6', '8', '10', '12'] [add(item) for item in x] *Is the same as... * def add(number): print 1 + int(number) x = ['2', '4', '6', '8', '10', '12'] map(add, x) They both yield the same results. Have you tried your own code? If I add one print() for each result and run the code, here is the output by me: 3 5 7 9 11 13 [None, None, None, None, None, None] Certainly these are not "the same results". And probably neither of them is the result you expected. I guess you go on using very imprecise, in fact wrong, terminology, and this drives you into thinking wrongly. There also are worng terms in your code itself, already signaled bu other (but you did not correct or even take into account, apparently), and consequent errors of thinking: * the "add" function does not "add" * in fact it does not _produce_ anything (instead it is an action that performs an effect, namely writing something onto the terminal) ... * ...so that using it as loop function in map simply makes no sense: map collect the results (products) of a function -- if that function produces results * this is why we get [None, None...]: a function (the term is wrong, it's actually say an "action") that does not produce but performs an effect return None by convention in Python. * "number" is not a number, but hopefully) the written expression of a number, whay is technically called a numeral (see wikipedia) Is there a benefit to using one way over the other? In larger computations, does one way calculate faster or is it merely a preference? Again, thank you. AngryNinja ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list comprehension equivalent to map(function, list item)
Bo Morris wrote: > Thank you for your assistance. Based on your direction, I figured it out. > > *This... * > > def add(number): > print 1 + int(number) > > x = ['2', '4', '6', '8', '10', '12'] > > [add(item) for item in x] > > *Is the same as... * > > > def add(number): > print 1 + int(number) > > x = ['2', '4', '6', '8', '10', '12'] > > map(add, x) > > They both yield the same results. Is there a benefit to using one way over > the other? In larger computations, does one way calculate faster or is it > merely a preference? Again, thank you. For built-in functions map(f, items) is a bit faster. List-comps are more flexible; you can inline the function >>> [int(s) + 1 for s in x] [3, 5, 7, 9, 11, 13] or add a filter: >>> [int(s) + 1 for s in x if set("12") & set(s)] [3, 11, 13] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list comprehension equivalent to map(function, list item)
Thank you for your assistance. Based on your direction, I figured it out. *This... * def add(number): print 1 + int(number) x = ['2', '4', '6', '8', '10', '12'] [add(item) for item in x] *Is the same as... * def add(number): print 1 + int(number) x = ['2', '4', '6', '8', '10', '12'] map(add, x) They both yield the same results. Is there a benefit to using one way over the other? In larger computations, does one way calculate faster or is it merely a preference? Again, thank you. AngryNinja On Fri, Dec 13, 2013 at 9:24 PM, Amit Saha wrote: > On Sat, Dec 14, 2013 at 11:03 AM, Bo Morris wrote: > > i have the following simple function that iterates over the list. It > passes > > the list item into the function and adds the numbers. What would be the > > equivalent way of writing the "map" portion with list comprehension? My > code > > is as follows: > > > > def add(number): > > print 1 + int(number) > > > > > > > > x = ['2', '4', '6', '8', '10', '12'] > > > > map(add, x) > > Think of a list comprehension as: > > [ dosomething(item) for item in alist] > > And, comparing it with your map implementation, here is what you get: > > >>> [1+int(item) for item in x] > [3, 5, 7, 9, 11, 13] > > > Here, dosomething(item) corresponds to 1+int(item). > > Hope that helps. > > -Amit. > > > -- > http://echorand.me > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list comprehension equivalent to map(function, list item)
On Fri, Dec 13, 2013 at 08:03:57PM -0500, Bo Morris wrote: > i have the following simple function that iterates over the list. Actually, no it doesn't. One important skill of being a programmer is precision of language. The function "add" you show below does not iterate over the list, it is the *map* function which does the iteration. > It passes the list item into the function and adds the numbers. Again, not so much. When you talk about adding up the numbers, given numbers like 5, 3, 2 I would expect to get 10 as the answer. That is not what your function does: it adds one to *each* number, alone. Now that I've lectured you pedantically on precision of language, which I hope you'll take in the constructive spirit it is intended, let me answer your actual question: > What would be the > equivalent way of writing the "map" portion with list comprehension? My > code is as follows: > > def add(number): > print 1 + int(number) > > x = ['2', '4', '6', '8', '10', '12'] > map(add, x) Converting a map to a list comprehension is simple: map(function, items) becomes: [function(item) for item in items] So your example simply becomes [add(s) for s in list_of_strings] A couple of other points: (1) The more work a variable is used for, the more descriptive its name should be. Variables which are used once can be a single letter. Temporary variables which don't last very long also can be a single letter. It is conventional to use a few single letter names: i, j, k: loop variables n, m: integers x, y: floats or decimals s: strings but only when they represent generic values. If possible, you should give variables names which explain *what they are* (such as "list_of_strings") or even better, *what they are used for* (such as "scores", "width", "number_of_pages", etc.) (2) In your example, your "add" function actually does two things: - it *calculates* a result (adding one to a number); - it *displays* that result (print). In general, it is best to keep those two parts separate. Why? Because good, effective programming involves putting parts together to make bigger parts. Once you introduce a print into a function, you can't really combine that part into a new more powerful part. You are now committed to *only* printing the calculation result, even if what you actually want to do is to perform more calculations on it. An example: suppose that, after adding one, you then want to double the result. You might think that you could do this: def double(number): print 2*number double(add(20)) That's exactly the sort of putting building blocks together that programming is all about. But if you try it, you'll see that it doesn't work. You'll get a mysterious error something like this: TypeError: unsupported operand type(s) for *: 'int' and 'NoneType' Why? Because your "add" function takes the calculated result and prints it, then throws the result away. Since the function doesn't return a value for later use, Python automatically returns the special None value, which you can think of as meaning something like "nothing at all". What happens when you try to double None? You get an error. The way to fix this and write functions which can be used as building blocks is to use "return" instead of "print", then call print at the end, only when you want to actually see something: def add(number): return 1 + int(number) x = ['2', '4', '6', '8', '10', '12'] print map(add, x) def double(number): return 2*number print double(add(20)) If you have any questions, please don't hesitate to ask! -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list comprehension equivalent to map(function, list item)
On Sat, Dec 14, 2013 at 11:03 AM, Bo Morris wrote: > i have the following simple function that iterates over the list. It passes > the list item into the function and adds the numbers. What would be the > equivalent way of writing the "map" portion with list comprehension? My code > is as follows: > > def add(number): > print 1 + int(number) > > > > x = ['2', '4', '6', '8', '10', '12'] > > map(add, x) Think of a list comprehension as: [ dosomething(item) for item in alist] And, comparing it with your map implementation, here is what you get: >>> [1+int(item) for item in x] [3, 5, 7, 9, 11, 13] Here, dosomething(item) corresponds to 1+int(item). Hope that helps. -Amit. -- http://echorand.me ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list comprehension equivalent to map(function, list item)
On 14/12/2013 01:03, Bo Morris wrote: i have the following simple function that iterates over the list. It passes the list item into the function and adds the numbers. What would be the equivalent way of writing the "map" portion with list comprehension? My code is as follows: def add(number): print 1 + int(number) x = ['2', '4', '6', '8', '10', '12'] map(add, x) thanks for the help and thank you for this mailing list. AngryNinja I don't see any function that iterates over anything. I do see a function that takes something called number (IMHO a very poor name), converts it into an int, adds 1 to it, prints it out and then returns None, the default when no return statement is given in a function. So change print to return, add it all up (very loud groan :) and you have. def add(number): return 1 + int(number) y = [add(z) for z in x] -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] list comprehension equivalent to map(function, list item)
i have the following simple function that iterates over the list. It passes the list item into the function and adds the numbers. What would be the equivalent way of writing the "map" portion with list comprehension? My code is as follows: def add(number): print 1 + int(number) x = ['2', '4', '6', '8', '10', '12'] map(add, x) thanks for the help and thank you for this mailing list. AngryNinja ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor