Am 10.05.2017 um 14:18 schrieb Chris Angelico: > On Wed, May 10, 2017 at 10:11 PM, Andre Müller <[email protected]> wrote: >> 1.) a short example for Python 3, but not exactly what they want. >> >> def square(numbers): >> yield from sorted(n**2 for n in numbers) >> >> numberlist = [99, 4, 3, 5, 6, 7, 0] >> result = list(square(numberlist)) > If you're going to use sorted(), why not simply return the list > directly? This unnecessarily iterates through the list and builds a > new one. > > ChrisA You're right. This can handle infinite sequences:
def square(numbers):
yield from (n**2 for n in numbers)
My example before can't do this.
Sorted consumes the whole list, there is no benefit.
*Ontopic*
Make a new empty list and iterate over your input_list,
do inside the loop the math operation and
append the result to the new list. Return the new list.
Hint1: The text above is longer as the resulting Code.
Hint2: Write this as a function (reusable code)
Hint3: Write this as a list comprehension.
Greetings
Andre
signature.asc
Description: OpenPGP digital signature
-- https://mail.python.org/mailman/listinfo/python-list
