简体   繁体   English

Python中列表的平均值

[英]average of the list in Python

I have a problem: i need to find an average of the list using this scheme: 我有一个问题:我需要使用此方案找到列表的平均值:

First of all, we find an average of two elements, three elements..... len(list) elements and form a new list using averages. 首先,我们找到两个元素的平均值,三个元素..... len(list)元素,并使用平均值形成一个新列表。 The use .pop() and find all averages again. 使用.pop()并再次找到所有平均值。 Function should stop when len(list) == 2 . len(list) == 2时,函数应停止。 Recursion should be used. 应该使用递归。

Example: list: [-1, 4, 8, 1] 范例:清单: [-1, 4, 8, 1]

1 step: 第1步:

  • find an average of [-1, 4], [-1, 4, 8], [-1, 4, 8, 1] 求出平均值[-1, 4], [-1, 4, 8], [-1, 4, 8, 1]
  • Then we form a new list: [1.5, 3.66..., 3] (averages) 然后我们形成一个新列表: [1.5, 3.66..., 3] (平均值)
  • Then find averages of new list: [1.5, 3.66...], [1.5, 3.66..., 3] 然后找到新列表的平均值: [1.5, 3.66...], [1.5, 3.66..., 3]
  • Then we form a new list: [2.5833.., 7.222...] (averages) 然后我们形成一个新列表: [2.5833.., 7.222...] (平均值)
  • When len(list) == 2 , find an average of this two elements. len(list) == 2 ,求这两个元素的平均值。

Answer is 2.652777 . 答案是2.652777

What should i write: 我应该写什么:

jada = []

while True:    
    print 'Lst elements:'    
    a = input()
    if (a == ''):    
        break    
    jada.append(a)

print 'Lst is:' + str(Jada)

def keskmine(Jada):
    for i in range(len(Jada) - 1):
        ...

    jada.pop()
    return keskmine(Jada)

Actually, this is a part of a homework, but i don't know how to solve it. 实际上,这是家庭作业的一部分,但我不知道如何解决。

Accept the list as the function argument. 接受列表作为函数参数。 If the list has one item, return that. 如果列表中有一项,则将其返回。 Create two iterators from the list. 从列表中创建两个迭代器。 Pop one item off one of the lists, zip them together, then find the averages of the zip results. 从列表之一中弹出一项,将它们压缩在一起,然后找到压缩结果的平均值。 Recurse. 递归。

In short, you're finding the "running average" from a list of numbers. 简而言之,您可以从数字列表中找到“运行平均值”。

Using recursion would be helpful here. 在这里使用递归会有所帮助。 Return the only element when "len(lst) == 1" otherwise, compute the running average and recurse. 当“ len(lst)== 1”时返回唯一的元素,否则,计算运行平均值并递归。

There are two parts in this assignment. 此作业分为两部分。 First, you need to transform lists like [-1, 4, 8, 1] to lists like [1.5, 3.66, 3] (find the running averages). 首先,您需要将[-1,4,8,1]之类的列表转换为[1.5,3.66,3]之类的列表(查找运行平均值)。 Second, you need to repeat this process with the result of the running averages until your list's length is 2 (or 1). 其次,您需要使用移动平均值的结果重复此过程,直到列表的长度为2(或1)。

You can tackle the first problem (find the running averages) independently from the second. 您可以独立于第二个问题来解决第一个问题(查找运行平均值)。 Finding the running average is simple, you first keep track of the running sum (eg if the list is [-1, 4, 8, 1] the running sum is [-1, 3, 11, 12]) and divide each elements by their respective running index (ie just [1, 2, 3, 4]), to get [-1/1, 3/2, 11/3, 12/4] = [-1, 1.5, 3.66, 3]. 找到运行平均值很简单,您首先要跟踪运行总和(例如,如果列表是[-1、4、8、1],运行总和是[-1、3、11、12]),然后将每个元素相除通过它们各自的运行索引(即[1、2、3、4]),得出[-1 / 1、3 / 2、11 / 3、12 / 4] = [-1、1.5、3.66、3] 。 Then you can discard the first element to get [1.5, 3.66, 3]. 然后,您可以舍弃第一个元素以获得[1.5,3.66,3]。

The second problem can be easily solved using recursion. 使用递归可以轻松解决第二个问题。 Recursion is just another form of looping, all recursive code can be transformed to a regular for/while-loops code and all looping code can be transformed to recursive code. 递归只是循环的另一种形式,所有递归代码都可以转换为常规的for / while循环代码,而所有循环代码都可以转换为递归代码。 However, some problems have a tendency towards a more "natural" solution in either recursion or looping. 但是,某些问题倾向于在递归或循环中采用更“自然”的解决方案。 In my opinion, the second problem (repeating the process of taking running averages) is more naturally solved using recursion. 在我看来,第二个问题(重复获取移动平均值的过程)可以使用递归更自然地解决。 Let's assume you have solved the first problem (of finding the running average) and we have a function runavg(lst) to solve the first problem. 假设您已经解决了第一个问题(找到运行平均值),并且我们有一个函数runavg(lst)来解决第一个问题。 We want to write a function which repeatedly find the running average of lst, or return the average when the lst's length is 2. 我们想要编写一个函数,该函数可以反复查找lst的运行平均值,或者在lst的长度为2时返回平均值。

This is also an opportunity to use python 3.x itertools.accumulate : 这也是使用python 3.x itertools.accumulate的机会:

From docs: 从文档:

>>> list(accumulate(8, 2, 50))

[8, 10, 60]

Then, you only need to divide each item by its index increased by 1, eliminate the first element and repeat until finished 然后,您只需要将每个项目除以其索引加1,就消除第一个元素并重复直到完成

For example, this works for any list of any length, doing most of the above-indicated steps inside a list comprehension: 例如,此方法适用于任何长度的任何列表,并在列表理解内执行上述大多数步骤:

>>> from itertools import accumulate
>>> a = [-1, 4, 8, 1]
>>> while len(a) > 1:
    a = [item / (index + 1) for (index, item) in enumerate(accumulate(a)) if index > 0]

>>> print(a)
[2.6527777777777777]

First I'll give you an explanation, and then some pseudo code, which you'll have to rewrite in Python. 首先,我将给您一个解释,然后是一些伪代码,您必须使用Python重写这些伪代码。 The main idea is to have one function that calls itself passing a lesser problem with each iteration. 主要思想是拥有一个函数,该函数在每次迭代时都会传递一个较小的问题。 In this case you would like to decrease the number of items by 1. 在这种情况下,您希望将项目数减少1。

You can either make a new list with every call, or reuse the same one if you'd like. 您可以在每次通话中创建一个新列表,也可以根据需要重复使用相同的列表。 Before passing on the list to the next iteration, you will need to calculate the averages thus creating a shorter list. 在将列表传递到下一个迭代之前,您需要计算平均值,从而创建一个较短的列表。

The idea is that you sum the numbers in a parameter and divide by the number of items you've added so far into the appropriate index in the list. 想法是将参数中的数字相加,然后除以到目前为止已添加到列表中适当索引中的项数。 Once you are done, you can pop the last item out. 完成后,您可以弹出最后一项。

The code should look something like this: (indexes in sample are zero based) 代码应如下所示:(样本中的索引从零开始)

average(list[])
    if(list.length == 0)  // Check input and handle errors
        exit
    if(list.length == 1)  // Recursion should stop
        return list[0]    // The one item is it's own average!
    // calculate the averages into the list in indices 0 to length - 2
    list.pop()                       // remove the last value
    return average(list)             // the recursion happens here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM