[Tutor] List comprehension question
Richard D. Moores
rdmoores at gmail.com
Mon Nov 8 02:15:57 CET 2010
On Sun, Nov 7, 2010 at 16:41, Wayne Werner <waynejwerner at gmail.com> wrote:
> On Sun, Nov 7, 2010 at 6:31 PM, Hugo Arts <hugo.yoshi at gmail.com> wrote:
>>
>> <snip>
>> here's a list comprehension
>> >>> a = [x*2 for x in range(10)]
>> >>> a
>> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>
>> here's the equivalent generator expression:
>> >>> a = (x*2 for x in range(10))
>>
>> <snip>
>
> Since you're talking about generators and efficiency, it's probably a good
> idea to point out that range is only a generator in python 3.x. In python
> 2.x it creates a list.
I should have mentioned that I'm using 3.1 .
So this version of my function uses a generator, range(), no?
def proper_divisors(n):
sum_ = 0
for x in range(1,n):
if n % x == 0:
sum_ += x
return sum_
Dick
More information about the Tutor
mailing list