【问题标题】:How to calculate the mean and SD from multiple lists in a dictionary and sort them into a new dictionary?如何从字典中的多个列表中计算平均值和 SD 并将它们排序到新字典中?
【发布时间】:2022-01-21 10:19:00
【问题描述】:

我有一个包含 5 个键的字典,每个键都有 10 个以列表形式分配给它的值:

{'a': [5, 6, 3, 1, 3, 2, 5, 8, 6, 7], 
 'b': [3, 5, 2, 7, 0, 2, 10, 4, 3, 4], 
 'c': [9, 7, 11, 10, 8, 9, 7, 10, 7, 9], 
 'd': [6, 4, 5, 7, 6, 8, 5, 6, 5, 7], 
 'e': [2, 5, 1, 4, 2, 3, 4, 2, 5, 1]}

我还设置了一个空字典,我想将每个列表的平均值和标准差存储在原始字典中:

{'a_analysis': [],
 'b_analysis': [],
 'c_analysis': [],
 'd_analysis': [],
 'e_analysis': []}

到目前为止我有这个代码:

for key in original_dictionary:   #for each key in the dictionary
   for value in original_dictionary:   #iterate through the values in each key
      sum =+ value   #add the value to a sum variable
   mean = sum / len(orignal_dictionary[key])   #get the mean by dividing the sum by the len of each key
   #here I want to return the mean value to the respective key in the new dictionary 
   sd =    #then I need to get a value for the standard deviation here 
   #and also return it to the respective key in the new dictionary

任何帮助将不胜感激。

【问题讨论】:

    标签: python list dictionary for-loop


    【解决方案1】:

    只是我的解决方案又小了一点,并添加了一些 cmets。 希望现在得到解释。

    import statistics
    
    input = {'a': [5, 6, 3, 1, 3, 2, 5, 8, 6, 7], 
     'b': [3, 5, 2, 7, 0, 2, 10, 4, 3, 4], 
     'c': [9, 7, 11, 10, 8, 9, 7, 10, 7, 9], 
     'd': [6, 4, 5, 7, 6, 8, 5, 6, 5, 7], 
     'e': [2, 5, 1, 4, 2, 3, 4, 2, 5, 1]}
    
    analyse = {'a_analysis': [],
     'b_analysis': [],
     'c_analysis': [],
     'd_analysis': [],
     'e_analysis': []}
    
    for key in input.keys(): #We look up every key we have in the directory "input"
        mean = sum(input[key]) / len(input[key]) #We calculate the mean by summing up all the values for the current key and dividing them through the length of our array 
        sd = statistics.stdev(input[key]) #We simply calculate the sd with a function from the libarary "statistics" 
        analyse[key+'_analysis'] = ["mean: "+str(mean), "sd: "+str(sd)] #We put the mean and sd into the directory "analyse" at the key our_current_key+"_analyse"
    
    for item in analyse.items(): print(item)
    

    【讨论】:

    • 您的答案可以通过添加有关代码的作用以及它如何帮助 OP 的更多信息来改进。
    【解决方案2】:

    这里有一个解决方案,希望你喜欢:

    def standard_deviation(numbers):
        mean = sum(numbers) / len(numbers)
        result = (sum( [((x - mean) ** 2) for x in numbers] ) / len(numbers))**0.5
        return result
    
    input ={
    'a': [5, 6, 3, 1, 3, 2, 5, 8, 6, 7],
    'b': [3, 5, 2, 7, 0, 2, 10, 4, 3, 4],
    'c': [9, 7, 11, 10, 8, 9, 7, 10, 7, 9],
    'd': [6, 4, 5, 7, 6, 8, 5, 6, 5, 7],
    'e': [2, 5, 1, 4, 2, 3, 4, 2, 5, 1]
    }
    output = {}
    for key in input.keys():
        output[key+"_analysis"] = {"mean": sum(input[key])/len(input[key]),
                                   "standard deviation":standard_deviation(input[key]),
                                   }
    

    这是输出:

    {'a_analysis': {'mean': 4.6, 'standard deviation': 2.1540659228538015},
     'b_analysis': {'mean': 4.0, 'standard deviation': 2.6832815729997477},
     'c_analysis': {'mean': 8.7, 'standard deviation': 1.345362404707371},
     'd_analysis': {'mean': 5.9, 'standard deviation': 1.1357816691600546},
     'e_analysis': {'mean': 2.9, 'standard deviation': 1.445683229480096}}
    

    【讨论】:

      【解决方案3】:

      您也可以为此使用pandas

      import pandas as pd
      out = pd.DataFrame(your_dict).agg(['mean', 'std']).to_dict()
      

      输出:

      {'a': {'mean': 4.6, 'std': 2.2705848487901865},
       'b': {'mean': 4.0, 'std': 2.8284271247461903},
       'c': {'mean': 8.7, 'std': 1.4181364924121764},
       'd': {'mean': 5.9, 'std': 1.1972189997378646},
       'e': {'mean': 2.9, 'std': 1.5238839267549948}}
      

      【讨论】:

        猜你喜欢
        • 2020-02-13
        • 1970-01-01
        • 2021-12-24
        • 1970-01-01
        • 2021-12-03
        • 1970-01-01
        • 1970-01-01
        • 2021-09-07
        • 2019-03-12
        相关资源
        最近更新 更多