Skip to main content
 首页 » 编程设计

python之与给定列表中的每个元素相比,查找每个整数的平均值

2025年05月04日14tuyile006

所以我正在练习对 3 人的 21 个问题进行调查的数据处理。我需要给出每个 # 给出的平均答案。我不确定如何在省略字母的同时分开数字和比较。

name=["AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5", 
      "BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2", 
      "K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1"] 

例如。 1=4.33

我的尝试:

def most_frequent(name):   
 
    counter = 0 
    num = name[0]   
    for i in range (len(name)):  
        curr_frequency = name[0].count(str(i))  
        if(curr_frequency> counter):  
            counter = curr_frequency  
            num = i  
    return num 

请您参考如下方法:

你可以试试这个:

name=["AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5", 
      "BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2", 
      "K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1"] 
 
for line in name : 
    parts = line.split()  # using space as a separator 
    word = parts[0]       # extract the word 
    numbers = map( float, parts[1:] )   # convert the numbers 
 
    print( word, numbers ) 
    # now you may calculate whatever you want =)