Ask Your Question

Revision history [back]

This behaviour is part of Python (the following code is evaluated in the standard python shell):

>>> W = [1, 8, 4, 7, 10, 1, 6, 3]
>>> 2*W
[1, 8, 4, 7, 10, 1, 6, 3, 1, 8, 4, 7, 10, 1, 6, 3]

The standard thing to do is to either use list comprehension

>>> [3*i for i in W]
[3, 24, 12, 21, 30, 3, 18, 9]

or map (for a comparison, see http://stackoverflow.com/q/1247486/421225)

>>> map(lambda i: 3*i, W)
[3, 24, 12, 21, 30, 3, 18, 9]

Although, if you're using just numerical lists and are used to matlab, then maybe you should use numpy:

>>> import numpy
>>> npW = numpy.array(W)
>>> npW*3
array([ 3, 24, 12, 21, 30,  3, 18,  9])

For large arrays, numpy will be faster at this type of operation than the pythonic methods above.

This behaviour is part of Python (the following code is evaluated in the standard python shell):

>>> W = [1, 8, 4, 7, 10, 1, 6, 3]
>>> 2*W
[1, 8, 4, 7, 10, 1, 6, 3, 1, 8, 4, 7, 10, 1, 6, 3]

The standard thing to do is to either use list comprehension

>>> [3*i for i in W]
[3, 24, 12, 21, 30, 3, 18, 9]

or map (for a comparison, see http://stackoverflow.com/q/1247486/421225)

>>> map(lambda i: 3*i, W)
[3, 24, 12, 21, 30, 3, 18, 9]

Although, if you're using just numerical lists and are used to matlab, then maybe you should use numpy:

>>> import numpy
>>> npW = numpy.array(W)
>>> npW*3
array([ 3, 24, 12, 21, 30,  3, 18,  9])

For large arrays, numpy will be faster at this type of operation than the pythonic methods above.

Of course, the sage approach is to use a vector object (see Laurent's answer)

This behaviour is part of Python (the following code is evaluated in the standard python shell):

>>> W = [1, 8, 4, 7, 10, 1, 6, 3]
>>> 2*W
[1, 8, 4, 7, 10, 1, 6, 3, 1, 8, 4, 7, 10, 1, 6, 3]

The standard thing to do is to either use list comprehension

>>> [3*i for i in W]
[3, 24, 12, 21, 30, 3, 18, 9]

or map (for a comparison, see http://stackoverflow.com/q/1247486/421225)

>>> map(lambda i: 3*i, W)
[3, 24, 12, 21, 30, 3, 18, 9]

Although, if you're using just numerical lists and are used to matlab, then maybe you should use numpy:

>>> import numpy
>>> npW = numpy.array(W)
>>> npW*3
array([ 3, 24, 12, 21, 30,  3, 18,  9])

For large arrays, numpy will be faster at this type of operation than the pythonic methods above.


Of course, the sage approach is to use a vector object (see Laurent's answer)

This behaviour is part of Python (the following code is evaluated in the standard python shell):shell, although it should also work in sage or a sage notebook):

>>> W = [1, 8, 4, 7, 10, 1, 6, 3]
>>> 2*W
[1, 8, 4, 7, 10, 1, 6, 3, 1, 8, 4, 7, 10, 1, 6, 3]

The standard thing to do is to either use list comprehension

>>> [3*i for i in W]
[3, 24, 12, 21, 30, 3, 18, 9]

or map (for a comparison, see http://stackoverflow.com/q/1247486/421225)

>>> map(lambda i: 3*i, W)
[3, 24, 12, 21, 30, 3, 18, 9]

Although, if you're using just numerical lists and are used to matlab, then maybe you should use numpy:

>>> import numpy
>>> npW = numpy.array(W)
>>> npW*3
array([ 3, 24, 12, 21, 30,  3, 18,  9])

For large arrays, numpy will be faster at this type of operation than the pythonic methods above.


Of course, the sage approach is to use a vector object (see Laurent's answer)

This behaviour is part of Python (the following code is evaluated in the standard python shell, although it should also work in sage or a sage notebook):

>>> W = [1, 8, 4, 7, 10, 1, 6, 3]
>>> 2*W
[1, 8, 4, 7, 10, 1, 6, 3, 1, 8, 4, 7, 10, 1, 6, 3]

The standard thing to do is to either use list comprehension

>>> [3*i for i in W]
[3, 24, 12, 21, 30, 3, 18, 9]

or map (for a comparison, see http://stackoverflow.com/q/1247486/421225)

>>> map(lambda i: 3*i, W)
[3, 24, 12, 21, 30, 3, 18, 9]

Although, if you're using just numerical lists and are used to matlab, then maybe you should use numpy:

>>> import numpy
>>> npW = numpy.array(W)
>>> npW*3
array([ 3, 24, 12, 21, 30,  3, 18,  9])

For large arrays, numpy NumPy will be faster at this type of operation than the pythonic methods above.above. For a discussion of NumPy in sage see http://www.sagemath.org/doc/numerical_sage/numpy.html


Of course, the standard sage approach is to use a vector object (see Laurent's answer)

answer)