>>> ListOfNumbers = [1,2,3,4,5,6,7,8,9,10]
>>> 1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9 / 10 # should be computed
2.75573192e
Asked
Active
Viewed 864 times
2 Answers
2
You can use reduce
:
ListOfNumbers = [1,2,3,4,5,6,7,8,9,10]
print(reduce(lambda x, y: x/float(y), ListOfNumbers))
Output:
2.7557319224e-07
You can also use itertools.accumulate
for Python3:
import operator
import itertools
print(list(itertools.accumulate(ListOfNumbers, func=operator.truediv))[-1])
Output:
2.7557319223985894e-07

Ajax1234
- 69,937
- 8
- 61
- 102
2
You could reduce
the list with a division operation. Note that since all the elements in your list are integers, you'd have to convert them to floats in order to use floating point division and get the result you expect:
result = reduce((lambda x, y: float(x) / y), [1,2,3,4,5,6,7,8,9,10])

Mureinik
- 297,002
- 52
- 306
- 350
-
1*"you'd have to convert them to floats in order to use floating point division"* - Only in Python 2. – vaultah Sep 22 '17 at 16:39