On 6/28/2013 10:38 AM, Vincent Davis wrote:
I have a list of a list of integers. The lists are long so i cant really
show an actual example of on of the lists, but I know that they contain
only the integers 1,2,3,4. so for example.
s2 = [[1,2,2,3,2,1,4,4],[2,4,3,2,3,1]]
I am calculating the product, sum, max, min.... of each list in s2 but I
get negative or 0 for the product for a lot of the lists. (I am doing
this in ipython)
Based on Python2 (from print output). Look at the underlying version to
make sure it is 2.7. Using Python 3 or something based on it is better
unless you *really* have to use Python 2.
for x in s2:
print('len = ', len(x), 'sum = ', sum(x), 'prod = ', prod(x), 'max
= ', max(x), 'min = ', min(x))
prod is not a Python builtin. I am guessing ipython adds it as a C-coded
builtin because a Python-coded function* would not have the overflow bug
exhibited below. See for instance
https://en.wikipedia.org/wiki/Integer_overflow
Not having to worry about this, because Python comes with
multi-precision integers, is a great thing about using Python rather
than almost any other language.
* I do not remember it this was always true for old enough Pythons.
('len = ', 100, 'sum = ', 247, 'prod = ', 0, 'max = ', 4, 'min = ', 1)
('len = ', 100, 'sum = ', 230, 'prod = ', -4611686018427387904, 'max = ', 4,
'min = ', 1)
def prod(seq):
res=1
for i in seq:
res *= i
return res
should work for you.
--
Terry Jan Reedy
--
http://mail.python.org/mailman/listinfo/python-list