Practice Python

Andre Müller gbs.deadeye at gmail.com
Wed May 10 12:10:07 EDT 2017



Am 10.05.2017 um 14:18 schrieb Chris Angelico:
> On Wed, May 10, 2017 at 10:11 PM, Andre Müller <gbs.deadeye at gmail.com> 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 866 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20170510/3c6bb78e/attachment.sig>


More information about the Python-list mailing list