# 函数式编程中的“ pythonic”等同于“ fold”函数是什么？

## 问题：函数式编程中的“ pythonic”等同于“ fold”函数是什么？

``````foldl (+) 0 [1,2,3,4,5]
--> 15
``````

``````[1,2,3,4,5].inject(0) {|m,x| m + x}
#> 15
``````

What is the most idiomatic way to achieve something like the following, in Haskell:

``````foldl (+) 0 [1,2,3,4,5]
--> 15
``````

Or its equivalent in Ruby:

``````[1,2,3,4,5].inject(0) {|m,x| m + x}
#> 15
``````

Obviously, Python provides the `reduce` function, which is an implementation of fold, exactly as above, however, I was told that the ‘pythonic’ way of programming was to avoid `lambda` terms and higher-order functions, preferring list-comprehensions where possible. Therefore, is there a preferred way of folding a list, or list-like structure in Python that isn’t the `reduce` function, or is `reduce` the idiomatic way of achieving this?

## 回答 0

``````def product(xs):
return reduce(operator.mul, xs, 1)``````

The Pythonic way of summing an array is using `sum`. For other purposes, you can sometimes use some combination of `reduce` (from the `functools` module) and the `operator` module, e.g.:

``````def product(xs):
return reduce(operator.mul, xs, 1)
``````

Be aware that `reduce` is actually a `foldl`, in Haskell terms. There is no special syntax to perform folds, there’s no builtin `foldr`, and actually using `reduce` with non-associative operators is considered bad style.

Using higher-order functions is quite pythonic; it makes good use of Python’s principle that everything is an object, including functions and classes. You are right that lambdas are frowned upon by some Pythonistas, but mostly because they tend not to be very readable when they get complex.

## 回答 1

`foldl (+) 0 [1,2,3,4,5]`

Python

`reduce(lambda a,b: a+b, [1,2,3,4,5], 0)`

`foldl (+) 0 [1,2,3,4,5]`

Python

`reduce(lambda a,b: a+b, [1,2,3,4,5], 0)`

Obviously, that is a trivial example to illustrate a point. In Python you would just do `sum([1,2,3,4,5])` and even Haskell purists would generally prefer `sum [1,2,3,4,5]`.

For non-trivial scenarios when there is no obvious convenience function, the idiomatic pythonic approach is to explicitly write out the for loop and use mutable variable assignment instead of using `reduce` or a `fold`.

That is not at all the functional style, but that is the “pythonic” way. Python is not designed for functional purists. See how Python favors exceptions for flow control to see how non-functional idiomatic python is.

## 回答 2

``````import operator, functools
def product(xs):
return functools.reduce(operator.mul, xs, 1)``````

``````def product(xs):
result = 1
for i in xs:
result *= i
return result``````

In Python 3, the `reduce` has been removed: Release notes. Nevertheless you can use the functools module

``````import operator, functools
def product(xs):
return functools.reduce(operator.mul, xs, 1)
``````

On the other hand, the documentation expresses preference towards `for`-loop instead of `reduce`, hence:

``````def product(xs):
result = 1
for i in xs:
result *= i
return result
``````

## 回答 3

``````def fold(f, l, a):
"""
f: the function to apply
l: the list to fold
a: the accumulator, who is also the 'zero' on the first call
"""
return a if(len(l) == 0) else fold(f, l[1:], f(a, l[0]))

print "Sum:", fold(lambda x, y : x+y, [1,2,3,4,5], 0)

print "Any:", fold(lambda x, y : x or y, [False, True, False], False)

print "All:", fold(lambda x, y : x and y, [False, True, False], True)

# Prove that result can be of a different type of the list's elements
print "Count(x==True):",
print fold(lambda x, y : x+1 if(y) else x, [False, True, True], 0)``````

You can reinvent the wheel as well:

``````def fold(f, l, a):
"""
f: the function to apply
l: the list to fold
a: the accumulator, who is also the 'zero' on the first call
"""
return a if(len(l) == 0) else fold(f, l[1:], f(a, l[0]))

print "Sum:", fold(lambda x, y : x+y, [1,2,3,4,5], 0)

print "Any:", fold(lambda x, y : x or y, [False, True, False], False)

print "All:", fold(lambda x, y : x and y, [False, True, False], True)

# Prove that result can be of a different type of the list's elements
print "Count(x==True):",
print fold(lambda x, y : x+1 if(y) else x, [False, True, True], 0)
``````

## 回答 4

``````a = [8,3,4]

## Foldl
reduce(lambda x,y: x**y, a)
#68719476736

## Foldr
reduce(lambda x,y: y**x, a[::-1])
#14134776518227074636666380005943348126619871175004951664972849610340958208L``````

Not really answer to the question, but one-liners for foldl and foldr:

``````a = [8,3,4]

## Foldl
reduce(lambda x,y: x**y, a)
#68719476736

## Foldr
reduce(lambda x,y: y**x, a[::-1])
#14134776518227074636666380005943348126619871175004951664972849610340958208L
``````

## 回答 5

``````items = [1, 2, 3, 4, 5]
f = lambda acc, x: acc * x
accumulator = 1``````

``````[accumulator := f(accumulator, x) for x in items]
# accumulator = 120``````

``````acc = 1; [acc := acc * x for x in [1, 2, 3, 4, 5]]
# acc = 120``````

``````acc = 1
scanned = [acc := acc * x for x in [1, 2, 3, 4, 5]]
# scanned = [1, 2, 6, 24, 120]
# acc = 120``````

Starting `Python 3.8`, and the introduction of assignment expressions (PEP 572) (`:=` operator), which gives the possibility to name the result of an expression, we can use a list comprehension to replicate what other languages call fold/foldleft/reduce operations:

Given a list, a reducing function and an accumulator:

``````items = [1, 2, 3, 4, 5]
f = lambda acc, x: acc * x
accumulator = 1
``````

we can fold `items` with `f` in order to obtain the resulting `accumulation`:

``````[accumulator := f(accumulator, x) for x in items]
# accumulator = 120
``````

or in a condensed formed:

``````acc = 1; [acc := acc * x for x in [1, 2, 3, 4, 5]]
# acc = 120
``````

Note that this is actually also a “scanleft” operation as the result of the list comprehension represents the state of the accumulation at each step:

``````acc = 1
scanned = [acc := acc * x for x in [1, 2, 3, 4, 5]]
# scanned = [1, 2, 6, 24, 120]
# acc = 120
``````

## 回答 6

``````initial_value = 0
for x in the_list:
initial_value += x #or any function.``````

The actual answer to this (reduce) problem is: Just use a loop!

``````initial_value = 0
for x in the_list:
initial_value += x #or any function.
``````

This will be faster than a reduce and things like PyPy can optimize loops like that.

BTW, the sum case should be solved with the `sum` function

## 回答 7

• 聚合的默认起始值​​或“种子”值。
• 该函数采用聚合的当前值（以“种子”开头）和列表中的下一个元素，并返回下一个聚合值。

I believe some of the respondents of this question have missed the broader implication of the `fold` function as an abstract tool. Yes, `sum` can do the same thing for a list of integers, but this is a trivial case. `fold` is more generic. It is useful when you have a sequence of data structures of varying shape and want to cleanly express an aggregation. So instead of having to build up a `for` loop with an aggregate variable and manually recompute it each time, a `fold` function (or the Python version, which `reduce` appears to correspond to) allows the programmer to express the intent of the aggregation much more plainly by simply providing two things:

• A default starting or “seed” value for the aggregation.
• A function that takes the current value of the aggregation (starting with the “seed”) and the next element in the list, and returns the next aggregation value.

## 回答 8

``````def foldr(func):
def accumulator(acc):
def listFunc(l):
if l:
x = l[0]
xs = l[1:]
return func(x)(foldr(func)(acc)(xs))
else:
return acc
return listFunc
return accumulator

def inner(y):
return x + y
return inner

def curried_mult(x):
def inner(y):
return x * y
return inner

print foldr(curried_mult)(1)(range(1, 6))``````

I may be quite late to the party, but we can create custom `foldr` using simple lambda calculus and curried function. Here is my implementation of foldr in python.

``````def foldr(func):
def accumulator(acc):
def listFunc(l):
if l:
x = l[0]
xs = l[1:]
return func(x)(foldr(func)(acc)(xs))
else:
return acc
return listFunc
return accumulator

def inner(y):
return x + y
return inner

def curried_mult(x):
def inner(y):
return x * y
return inner

print foldr(curried_mult)(1)(range(1, 6))
``````

Even though the implementation is recursive (might be slow), it will print the values `15` and `120` respectively

# NameError：名称“ reduce”未在Python中定义

## 问题：NameError：名称“ reduce”未在Python中定义

``````xor = lambda x,y: (x+y)%2
l = reduce(xor, [1,2,3,4])
``````

``````l = reduce(xor, [1,2,3,4])
NameError: name 'reduce' is not defined
``````

``NameError: name 'reduce' is not defined``

`reduce`在Python 3.2中真正删除的吗？如果是这样，还有什么选择？

I’m using Python 3.2. Tried this:

``````xor = lambda x,y: (x+y)%2
l = reduce(xor, [1,2,3,4])
``````

And got the following error:

``````l = reduce(xor, [1,2,3,4])
NameError: name 'reduce' is not defined
``````

Tried printing `reduce` into interactive console – got this error:

``````NameError: name 'reduce' is not defined
``````

Is `reduce` really removed in Python 3.2? If that’s the case, what’s the alternative?

## 回答 0

It was moved to `functools`.

## 回答 1

``from functools import reduce``

``````from functools import reduce
``````

before you use the reduce.

## 回答 2

``from six.moves import reduce``

Or if you use the six library

``````from six.moves import reduce
``````

## 回答 3

``l = sum([1,2,3,4]) % 2``

In this case I believe that the following is equivalent:

``````l = sum([1,2,3,4]) % 2
``````

The only problem with this is that it creates big numbers, but maybe that is better than repeated modulo operations?

# 如何在Python 3中使用过滤，映射和归约

## 问题：如何在Python 3中使用过滤，映射和归约

`filter`，，`map`并且`reduce`可以在Python 2中完美运行。这是一个示例：

``````>>> def f(x):
return x % 2 != 0 and x % 3 != 0
>>> filter(f, range(2, 25))
[5, 7, 11, 13, 17, 19, 23]

>>> def cube(x):
return x*x*x
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

return x+y
55
``````

``````>>> filter(f, range(2, 25))
<filter object at 0x0000000002C14908>

>>> map(cube, range(1, 11))
<map object at 0x0000000002C82B70>

Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
NameError: name 'reduce' is not defined
``````

`filter`, `map`, and `reduce` work perfectly in Python 2. Here is an example:

``````>>> def f(x):
return x % 2 != 0 and x % 3 != 0
>>> filter(f, range(2, 25))
[5, 7, 11, 13, 17, 19, 23]

>>> def cube(x):
return x*x*x
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

return x+y
55
``````

But in Python 3, I receive the following outputs:

``````>>> filter(f, range(2, 25))
<filter object at 0x0000000002C14908>

>>> map(cube, range(1, 11))
<map object at 0x0000000002C82B70>

Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
NameError: name 'reduce' is not defined
``````

I would appreciate if someone could explain to me why this is.

Screenshot of code for further clarity:

## 回答 0

• […]
• `map()``filter()`返回迭代器。如果您确实需要列表，则可以使用快速解决方案，例如`list(map(...))`，但是更好的解决方案通常是使用列表理解（特别是当原始代码使用lambda时），或者重写代码以使其根本不需要列表。`map()`该函数的副作用特别棘手。正确的转换是使用常规`for`循环（因为创建列表将很浪费）。
• […]

• […]
• 已删除`reduce()``functools.reduce()`如果确实需要，请使用；但是，在99％的时间里，显式`for`循环更易于阅读。
• […]

You can read about the changes in What’s New In Python 3.0. You should read it thoroughly when you move from 2.x to 3.x since a lot has been changed.

The whole answer here are quotes from the documentation.

Views And Iterators Instead Of Lists

Some well-known APIs no longer return lists:

• […]
• `map()` and `filter()` return iterators. If you really need a list, a quick fix is e.g. `list(map(...))`, but a better fix is often to use a list comprehension (especially when the original code uses lambda), or rewriting the code so it doesn’t need a list at all. Particularly tricky is `map()` invoked for the side effects of the function; the correct transformation is to use a regular `for` loop (since creating a list would just be wasteful).
• […]

Builtins

• […]
• Removed `reduce()`. Use `functools.reduce()` if you really need it; however, 99 percent of the time an explicit `for` loop is more readable.
• […]

## 回答 1

``````>>> def f(x): return x % 2 != 0 and x % 3 != 0
...
>>> list(filter(f, range(2, 25)))
[5, 7, 11, 13, 17, 19, 23]
>>> def cube(x): return x*x*x
...
>>> list(map(cube, range(1, 11)))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
>>> import functools
...
55
>>>``````

``````>>> def f(x): return x % 2 != 0 and x % 3 != 0
...
>>> [i for i in range(2, 25) if f(i)]
[5, 7, 11, 13, 17, 19, 23]
>>> def cube(x): return x*x*x
...
>>> [cube(i) for i in range(1, 11)]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
>>>``````

The functionality of `map` and `filter` was intentionally changed to return iterators, and reduce was removed from being a built-in and placed in `functools.reduce`.

So, for `filter` and `map`, you can wrap them with `list()` to see the results like you did before.

``````>>> def f(x): return x % 2 != 0 and x % 3 != 0
...
>>> list(filter(f, range(2, 25)))
[5, 7, 11, 13, 17, 19, 23]
>>> def cube(x): return x*x*x
...
>>> list(map(cube, range(1, 11)))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
>>> import functools
...
55
>>>
``````

The recommendation now is that you replace your usage of map and filter with generators expressions or list comprehensions. Example:

``````>>> def f(x): return x % 2 != 0 and x % 3 != 0
...
>>> [i for i in range(2, 25) if f(i)]
[5, 7, 11, 13, 17, 19, 23]
>>> def cube(x): return x*x*x
...
>>> [cube(i) for i in range(1, 11)]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
>>>
``````

They say that for loops are 99 percent of the time easier to read than reduce, but I’d just stick with `functools.reduce`.

Edit: The 99 percent figure is pulled directly from the What’s New In Python 3.0 page authored by Guido van Rossum.

## 回答 2

``````from contextlib import contextmanager

@contextmanager
def noiters(*funcs):
if not funcs:
funcs = [map, filter, zip] # etc
from functools import reduce
globals()[reduce.__name__] = reduce
for func in funcs:
globals()[func.__name__] = lambda *ar, func = func, **kwar: list(func(*ar, **kwar))
try:
yield
finally:
del globals()[reduce.__name__]
for func in funcs: globals()[func.__name__] = func``````

``````with noiters(map):
print(map(int, ['1', '2']))``````

``````190
[1, 2]``````

As an addendum to the other answers, this sounds like a fine use-case for a context manager that will re-map the names of these functions to ones which return a list and introduce `reduce` in the global namespace.

A quick implementation might look like this:

``````from contextlib import contextmanager

@contextmanager
def noiters(*funcs):
if not funcs:
funcs = [map, filter, zip] # etc
from functools import reduce
globals()[reduce.__name__] = reduce
for func in funcs:
globals()[func.__name__] = lambda *ar, func = func, **kwar: list(func(*ar, **kwar))
try:
yield
finally:
del globals()[reduce.__name__]
for func in funcs: globals()[func.__name__] = func
``````

With a usage that looks like this:

``````with noiters(map):
print(map(int, ['1', '2']))
``````

Which prints:

``````190
[1, 2]
``````

Just my 2 cents 🙂

## 回答 3

``````import functools
my_list = [10,15,20,25,35]
sum_numbers = functools.reduce(lambda x ,y : x+y , my_list)
print(sum_numbers)``````

Since the `reduce` method has been removed from the built in function from Python3, don’t forget to import the `functools` in your code. Please look at the code snippet below.

``````import functools
my_list = [10,15,20,25,35]
sum_numbers = functools.reduce(lambda x ,y : x+y , my_list)
print(sum_numbers)
``````

## 回答 4

//过滤

//地图

multipleOf2 = list（map（lambda x：x * 2，数字））

//降低

sumOfNumbers = reduce（lambda x，y：x + y，数字）

Here are the examples of Filter, map and reduce functions.

numbers = [10,11,12,22,34,43,54,34,67,87,88,98,99,87,44,66]

//Filter

oddNumbers = list(filter(lambda x: x%2 != 0, numbers))

print(oddNumbers)

//Map

multiplyOf2 = list(map(lambda x: x*2, numbers))

print(multiplyOf2)

//Reduce

The reduce function, since it is not commonly used, was removed from the built-in functions in Python 3. It is still available in the functools module, so you can do:

from functools import reduce

sumOfNumbers = reduce(lambda x,y: x+y, numbers)

print(sumOfNumbers)

## 回答 5

map，filter和reduce的优点之一是当您将它们“链接”在一起以进行复杂的操作时，它们变得清晰易读。但是，内置语法不清晰，全都是“向后的”。因此，我建议使用该`PyFunctional`软件包（https://pypi.org/project/PyFunctional/）。 这是两者的比较：

``flight_destinations_dict = {'NY': {'London', 'Rome'}, 'Berlin': {'NY'}}``

Py功能版本

“我有一个飞行目的地序列。如果城市位于dict值中，我想从中获得dict键。最后，过滤掉我在流程中创建的空列表。”

``````from functional import seq  # PyFunctional package to allow easier syntax

def find_return_flights_PYFUNCTIONAL_SYNTAX(city, flight_destinations_dict):
return seq(flight_destinations_dict.items()) \
.map(lambda x: x[0] if city in x[1] else []) \
.filter(lambda x: x != []) \``````

“好的，所以有一个列表。我想从中过滤出空列表。为什么？因为如果城市位于dict值中，我首先得到了dict键。哦，我要执行的列表是flight_destinations_dict。 ”

``````def find_return_flights_DEFAULT_SYNTAX(city, flight_destinations_dict):
return list(
filter(lambda x: x != [],
map(lambda x: x[0] if city in x[1] else [], flight_destinations_dict.items())
)
)``````

One of the advantages of map, filter and reduce is how legible they become when you “chain” them together to do something complex. However, the built-in syntax isn’t legible and is all “backwards”. So, I suggest using the `PyFunctional` package (https://pypi.org/project/PyFunctional/). Here’s a comparison of the two:

``````flight_destinations_dict = {'NY': {'London', 'Rome'}, 'Berlin': {'NY'}}
``````

PyFunctional version

Very legible syntax. You can say:

“I have a sequence of flight destinations. Out of which I want to get the dict key if city is in the dict values. Finally, filter out the empty lists I created in the process.”

``````from functional import seq  # PyFunctional package to allow easier syntax

def find_return_flights_PYFUNCTIONAL_SYNTAX(city, flight_destinations_dict):
return seq(flight_destinations_dict.items()) \
.map(lambda x: x[0] if city in x[1] else []) \
.filter(lambda x: x != []) \
``````

Default Python version

It’s all backwards. You need to say:

“OK, so, there’s a list. I want to filter empty lists out of it. Why? Because I first got the dict key if the city was in the dict values. Oh, the list I’m doing this to is flight_destinations_dict.”

``````def find_return_flights_DEFAULT_SYNTAX(city, flight_destinations_dict):
return list(
filter(lambda x: x != [],
map(lambda x: x[0] if city in x[1] else [], flight_destinations_dict.items())
)
)
``````

# 查找列表的平均值

## 问题：查找列表的平均值

``````l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print reduce(lambda x, y: x + y, l)``````

I have to find the average of a list in Python. This is my code so far

``````l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print reduce(lambda x, y: x + y, l)
``````

I’ve got it so it adds together the values in the list, but I don’t know how to make it divide into them?

## 回答 0

``````l = [15, 18, 2, 36, 12, 78, 5, 6, 9]

import statistics
statistics.mean(l)  # 20.11111111111111``````

``sum(l) / len(l)``

``sum(l) / float(len(l))``

On Python 3.4+ you can use `statistics.mean()`

``````l = [15, 18, 2, 36, 12, 78, 5, 6, 9]

import statistics
statistics.mean(l)  # 20.11111111111111
``````

On older versions of Python you can do

``````sum(l) / len(l)
``````

On Python 2 you need to convert `len` to a float to get float division

``````sum(l) / float(len(l))
``````

There is no need to use `reduce`. It is much slower and was removed in Python 3.

## 回答 1

``````l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
sum(l) / len(l)``````
``````l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
sum(l) / len(l)
``````

## 回答 2

``````l = [15, 18, 2, 36, 12, 78, 5, 6, 9]

import numpy as np
print(np.mean(l))``````

You can use `numpy.mean`:

``````l = [15, 18, 2, 36, 12, 78, 5, 6, 9]

import numpy as np
print(np.mean(l))
``````

## 回答 3

``````from statistics import mean
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
mean(l)``````

A statistics module has been added to python 3.4. It has a function to calculate the average called mean. An example with the list you provided would be:

``````from statistics import mean
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
mean(l)
``````

## 回答 4

`reduce()`当Python具有完美的`sum()`功能时，为什么要使用此功能？

``print sum(l) / float(len(l))``

`float()`必须强制Python执行浮点除法。）

Why would you use `reduce()` for this when Python has a perfectly cromulent `sum()` function?

``````print sum(l) / float(len(l))
``````

(The `float()` is necessary to force Python to do a floating-point division.)

## 回答 5

https://docs.python.org/3/library/statistics.html

``````list = [11, 13, 12, 15, 17]
import statistics as s
s.mean(list)``````

There is a statistics library if you are using python >= 3.4

https://docs.python.org/3/library/statistics.html

You may use it’s mean method like this. Let’s say you have a list of numbers of which you want to find mean:-

``````list = [11, 13, 12, 15, 17]
import statistics as s
s.mean(list)
``````

It has other methods too like stdev, variance, mode, harmonic mean, median etc which are too useful.

## 回答 6

``````def avg(l):
return sum(l, 0.0) / len(l)``````

Instead of casting to float, you can add 0.0 to the sum:

``````def avg(l):
return sum(l, 0.0) / len(l)
``````

## 回答 7

`sum(l) / float(len(l))` 是正确的答案，但仅出于完整性考虑，您可以通过一次减少来计算平均值：

``````>>> reduce(lambda x, y: x + y / float(len(l)), l, 0)
20.111111111111114``````

``````>>> sum(l) / float(len(l))
20.111111111111111``````

`sum(l) / float(len(l))` is the right answer, but just for completeness you can compute an average with a single reduce:

``````>>> reduce(lambda x, y: x + y / float(len(l)), l, 0)
20.111111111111114
``````

Note that this can result in a slight rounding error:

``````>>> sum(l) / float(len(l))
20.111111111111111
``````

## 回答 8

``````from statistics import mean

n = [11, 13, 15, 17, 19]

print(n)
print(mean(n))``````

I tried using the options above but didn’t work. Try this:

``````from statistics import mean

n = [11, 13, 15, 17, 19]

print(n)
print(mean(n))
``````

worked on python 3.5

## 回答 9

``pd.Series(sequence).mean()``

``````>>> import pandas as pd
>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> pd.Series(l).mean()
20.11111111111111
>>> ``````

`Series.mean(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)`

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.mean.html

https://pandas.pydata.org/pandas-docs/stable/10min.html

Or use `pandas`‘s `Series.mean` method:

``````pd.Series(sequence).mean()
``````

Demo:

``````>>> import pandas as pd
>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> pd.Series(l).mean()
20.11111111111111
>>>
``````

From the docs:

`Series.mean(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)`

And here is the docs for this:

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.mean.html

And the whole documentation:

https://pandas.pydata.org/pandas-docs/stable/10min.html

## 回答 10

``````def list_mean(n):

summing = float(sum(n))
count = float(len(n))
if n == []:
return False
return float(summing/count)``````

I had a similar question to solve in a Udacity´s problems. Instead of a built-in function i coded:

``````def list_mean(n):

summing = float(sum(n))
count = float(len(n))
if n == []:
return False
return float(summing/count)
``````

Much more longer than usual but for a beginner its quite challenging.

## 回答 11

``````L = [15, 18, 2, 36, 12, 78, 5, 6, 9]

total = 0

def average(numbers):
total = sum(numbers)
total = float(total)

print average(L)``````

as a beginner, I just coded this:

``````L = [15, 18, 2, 36, 12, 78, 5, 6, 9]

total = 0

def average(numbers):
total = sum(numbers)
total = float(total)

print average(L)
``````

## 回答 12

### 如果您想获得的不仅仅是平均值（也就是平均值），您可以查看scipy统计信息

``````from scipy import stats
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print(stats.describe(l))

# DescribeResult(nobs=9, minmax=(2, 78), mean=20.11111111111111,
# variance=572.3611111111111, skewness=1.7791785448425341,
# kurtosis=1.9422716419666397)``````

### If you wanted to get more than just the mean (aka average) you might check out scipy stats

``````from scipy import stats
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print(stats.describe(l))

# DescribeResult(nobs=9, minmax=(2, 78), mean=20.11111111111111,
# variance=572.3611111111111, skewness=1.7791785448425341,
# kurtosis=1.9422716419666397)
``````

## 回答 13

``````>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> running_average = reduce(lambda aggr, elem: (aggr[0] + elem, aggr[1]+1), l, (0.0,0))
>>> running_average[0]
(181.0, 9)
>>> running_average[0]/running_average[1]
20.111111111111111``````

In order to use `reduce` for taking a running average, you’ll need to track the total but also the total number of elements seen so far. since that’s not a trivial element in the list, you’ll also have to pass `reduce` an extra argument to fold into.

``````>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> running_average = reduce(lambda aggr, elem: (aggr[0] + elem, aggr[1]+1), l, (0.0,0))
>>> running_average[0]
(181.0, 9)
>>> running_average[0]/running_average[1]
20.111111111111111
``````

## 回答 14

``````>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> print reduce(lambda x, y: x + y, l) / len(l)
20
>>> sum(l)/len(l)
20``````

``````>>> print reduce(lambda x, y: x + y, l) / float(len(l))
20.1111111111
>>> print sum(l)/float(len(l))
20.1111111111``````

@安德鲁·克拉克（Andrew Clark）的发言是正确的。

Both can give you close to similar values on an integer or at least 10 decimal values. But if you are really considering long floating values both can be different. Approach can vary on what you want to achieve.

``````>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> print reduce(lambda x, y: x + y, l) / len(l)
20
>>> sum(l)/len(l)
20
``````

Floating values

``````>>> print reduce(lambda x, y: x + y, l) / float(len(l))
20.1111111111
>>> print sum(l)/float(len(l))
20.1111111111
``````

@Andrew Clark was correct on his statement.

## 回答 15

```x = [[-5.01,-5.43,1.08,0.86,-2.67,4.94,-2.51,-2.25,5.56,1.03], [-8.12,-3.48,-5.52,-3.78,0.63,3.29,2.09,-2.13,2.86,-3.33], [-3.68,-3.54,1.66,-4.11,7.39,2.08,-2.59,-6.94,-2.26,4.33]]```

``theMean = np.mean(x1,axis=1)``

suppose that

```x = [[-5.01,-5.43,1.08,0.86,-2.67,4.94,-2.51,-2.25,5.56,1.03], [-8.12,-3.48,-5.52,-3.78,0.63,3.29,2.09,-2.13,2.86,-3.33], [-3.68,-3.54,1.66,-4.11,7.39,2.08,-2.59,-6.94,-2.26,4.33]]```

you can notice that `x` has dimension 3*10 if you need to get the `mean` to each row you can type this

``````theMean = np.mean(x1,axis=1)
``````

don’t forget to `import numpy as np`

## 回答 16

``````l = [15, 18, 2, 36, 12, 78, 5, 6, 9]

l = map(float,l)
print '%.2f' %(sum(l)/len(l))``````
``````l = [15, 18, 2, 36, 12, 78, 5, 6, 9]

l = map(float,l)
print '%.2f' %(sum(l)/len(l))
``````

## 回答 17

``````l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print(sum(l)//len(l))``````

Find the average in list By using the following PYTHON code:

``````l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print(sum(l)//len(l))
``````

try this it easy.

## 回答 18

``print reduce(lambda x, y: x + y, l)/(len(l)*1.0)``

``sum(l)/(len(l)*1.0)``

1.0是确保获得浮点除法

``````print reduce(lambda x, y: x + y, l)/(len(l)*1.0)
``````

or like posted previously

``````sum(l)/(len(l)*1.0)
``````

The 1.0 is to make sure you get a floating point division

## 回答 19

``````from operator import truediv

L = [15, 18, 2, 36, 12, 78, 5, 6, 9]

def sum_and_count(x, y):
try:
return (x[0] + y, x[1] + 1)
except TypeError:
return (x + y, 2)

truediv(*reduce(sum_and_count, L))

# prints
20.11111111111111``````

Combining a couple of the above answers, I’ve come up with the following which works with reduce and doesn’t assume you have `L` available inside the reducing function:

``````from operator import truediv

L = [15, 18, 2, 36, 12, 78, 5, 6, 9]

def sum_and_count(x, y):
try:
return (x[0] + y, x[1] + 1)
except TypeError:
return (x + y, 2)

truediv(*reduce(sum_and_count, L))

# prints
20.11111111111111
``````

## 回答 20

``````import itertools,operator

I want to add just another approach

``````import itertools,operator
``````

## 回答 21

``````numbers = [0,1,2,3]

numbers[0] = input("Please enter a number")

numbers[1] = input("Please enter a second number")

numbers[2] = input("Please enter a third number")

numbers[3] = input("Please enter a fourth number")

print (numbers)

print ("Finding the Avarage")

avarage = int(numbers[0]) + int(numbers[1]) + int(numbers[2]) + int(numbers [3]) / 4

print (avarage)``````
``````numbers = [0,1,2,3]

numbers[0] = input("Please enter a number")

numbers[1] = input("Please enter a second number")

numbers[2] = input("Please enter a third number")

numbers[3] = input("Please enter a fourth number")

print (numbers)

print ("Finding the Avarage")

avarage = int(numbers[0]) + int(numbers[1]) + int(numbers[2]) + int(numbers [3]) / 4

print (avarage)
``````