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

标签 python

所以我正在练习对 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 =)

关于python - 与给定列表中的每个元素相比,查找每个整数的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59098191/

相关文章:

python - 使用 python 将标签标记为 LMDB 数据中的矩阵

python - 如何有效地打印 JSON 对象列表?

python - 如何在 Pandas 中存储多个相关的时间序列

python pycurl 相当于 curl -b get 命令

python - 如何使用 python selenium 单击弹出窗口中的按钮?

python - 如何多次运行相同的TestSuite unittest texttestrunner

python - 如何使用 python 类型指定这种可变参数元组?

python - 分配字母数值

python - 如何从命令行在 Python 中显示图像并提示输入短字符串

Python 3 解码字符串