Ask Your Question

Revision history [back]

the syntax for list comprehension with a condition is at the end, for example:

sage: [i for i in srange(-3, 3) if i^2 == 4]
[-2, 2]

then:

sage: L2 =[sum(divisors(i))-i for i in srange(1, 100, include_endpoint=True)]
sage: L3 = srange(1, 100, include_endpoint=True)
sage: L4 = [L2[i] for i in range(100) if L2[i] > L3[i]]
sage: L4
[16, 21, 22, 36, 42, 55, 50, 54, 76, 66, 64, 108, 78, 74, 123, 90, 106, 140, 92, 144, 156, 117]

for this use and further learning material, consider giving a try to the Programming in Python and Sage Thematic Tutorial. there are even exercises!

the syntax for list comprehension with a condition is at the end, for example:

sage: [i for i in srange(-3, 3) if i^2 == 4]
[-2, 2]

then:

sage: L2 =[sum(divisors(i))-i = [sum(divisors(i))-i for i in srange(1, 100, include_endpoint=True)]
sage: L3 = srange(1, 100, include_endpoint=True)
sage: L4 = [L2[i] for i in range(100) if L2[i] > L3[i]]
sage: L4
[16, 21, 22, 36, 42, 55, 50, 54, 76, 66, 64, 108, 78, 74, 123, 90, 106, 140, 92, 144, 156, 117]

for this use and further learning material, consider giving a try to the Programming in Python and Sage Thematic Tutorial. there are even exercises! exercises!

the syntax for list comprehension with a condition is at the end, for example:

sage: [i for i in srange(-3, 3) if i^2 == 4]
[-2, 2]

then:

sage: L2 = [sum(divisors(i))-i for i in srange(1, 100, include_endpoint=True)]
sage: L3 = srange(1, 100, include_endpoint=True)
sage: L4 = [L2[i] [L2i for i i, L2i in range(100) enumerate(L2) if L2[i] > L3[i]]
sage: L4
[16, 21, 22, 36, 42, 55, 50, 54, 76, 66, 64, 108, 78, 74, 123, 90, 106, 140, 92, 144, 156, 117]

for this use and further learning material, consider giving a try to the Programming in Python and Sage Thematic Tutorial. there are even exercises!

the syntax for list comprehension with a condition is at the end, for example:

sage: [i for i in srange(-3, 3) if i^2 == 4]
[-2, 2]

then:

sage: L2 = [sum(divisors(i))-i for i in srange(1, 100, include_endpoint=True)]
sage: L3 = srange(1, 100, include_endpoint=True)
sage: L4 = [L2i for i, L2i in enumerate(L2) if L2[i] > L3[i]]
sage: L4
[16, 21, 22, 36, 42, 55, 50, 54, 76, 66, 64, 108, 78, 74, 123, 90, 106, 140, 92, 144, 156, 117]

for this use and further learning material, consider giving a try to the Programming in Python and Sage Thematic Tutorial. there even are even exercises!

the syntax for list comprehension with a condition is at to write the end, if clause after the loop iterable, for example:

sage: [i for i in srange(-3, 3) if i^2 == 4]
[-2, 2]

then:

sage: L2 = [sum(divisors(i))-i for i in srange(1, 100, include_endpoint=True)]
sage: L3 = srange(1, 100, include_endpoint=True)
sage: L4 = [L2i for i, L2i in enumerate(L2) if L2[i] > L3[i]]
sage: L4
[16, 21, 22, 36, 42, 55, 50, 54, 76, 66, 64, 108, 78, 74, 123, 90, 106, 140, 92, 144, 156, 117]

for this use and further learning material, consider giving a try to the Programming in Python and Sage Thematic Tutorial. there even are exercises!

the syntax for list comprehension with a condition is to write the if clause after after the loop iterable, for example:

sage: [i for i in srange(-3, 3) if i^2 == 4]
[-2, 2]

then:

sage: L2 = [sum(divisors(i))-i for i in srange(1, 100, include_endpoint=True)]
sage: L3 = srange(1, 100, include_endpoint=True)
sage: L4 = [L2i for i, L2i in enumerate(L2) if L2[i] > L3[i]]
sage: L4
[16, 21, 22, 36, 42, 55, 50, 54, 76, 66, 64, 108, 78, 74, 123, 90, 106, 140, 92, 144, 156, 117]

for this use and further learning material, consider giving a try to the Programming in Python and Sage Thematic Tutorial. there even are exercises!